如何从txt文件中获取行并检查用户输入的内容是否正确

问题描述 投票:0回答:1

我正在创建一个银行应用程序。

到目前为止,我可以创建一个帐户。之后,它会对密码和 SSN 等数据进行加密。之后,我会进入登录功能,我必须输入我的用户名和密码。

我想读取

bankUsersFile
中的行以获取加密密码并将其设置为等于我输入的密码。我输入的密码也会快速加密,然后在
if
语句中将它们设置为相等。如果它们正确,它将调用主银行应用程序函数并打印
"you are in"

我尝试这样做:

void LoginToAccount()
{
   vector<int> LoginCodedPassword;
   string LoginPassword;
   string LoginUsername;
    
   ifstream b2("BankUsersFiles.txt"); 
   
   cout << "LOGIN" << endl;
   cout << "Please Enter Your Username" << endl;
   getline(cin, LoginUsername);
   for (getline(b2, UserData.Username) == LoginUsername)
   {
       cout << "Please Enter Your Password" << endl;
       getline(cin, LoginPassword);
       LoginCodedPassword = encoder(LoginPassword)
       if(LoginCodedPassword == getline(b2,UserData.codedPassword))
       {
           MainApplication();
       }
   }

我的创建账户功能:

void CreateUserAccount()
{
    ofstream b1("BankUsersFiles.txt", ios::app);
    int choice = 0;
    string VerifyPassTemp;
    
    cout << "1. Create New Account" << endl;
    cout << "2. Switch to Login" << endl;
    cout << "3. Exit Program" << endl;
    
    cin >> choice;
    switch(choice)
    {
        case 1:
        {
            cout << "Please Enter Your Username: " << endl;
            cin >> UserData.Username;
            cout << "Please Enter Your Password: " << endl;
            cin >> UserData.Password;
            cout << "Please verify your Password: " << endl;
            cin >> VerifyPassTemp;
            UserData.codedPassword = encoder(UserData.Password);
            if (VerifyPassTemp == UserData.Password)
            {
                UserData.LoginPassword = "0";
                cout << "Please Enter Your Current Adress: " << endl;
                cin >> UserData.Adress;
                cout << "Please Enter Your SSN" << endl;
                cin >> UserData.SSN;
                UserData.codedSSN = encoder(UserData.SSN);
                UserData.SSN = "0";
                
                b1 << UserData.Username << endl << UserData.codedPassword << endl << UserData.Adress << endl << UserData.codedSSN << endl;
            }
            else
            {
                break;
            }
            
            b1.close();
            
            LoginToAccount();
        }
        case 2:
        {
            LoginToAccount();
        }
    }
  
}

但我收到此错误:

“operator==”(操作数类型为“std::basic_istream”和“std::string”{aka“std::__cxx11::basic_string”})

我的编码器也工作正常。

我不知道如何解决这个问题!

另外,由于我的编码器,我正在使用

vector<int>

set<int>
    prime; // a set will be the collection of prime numbers,
           // where we can select random primes p and q
int public_key;
int private_key;
int n;
// we will run the function only once to fill the set of
// prime numbers

void MainApplication()
{
    cout << "You Are in!" << endl;
}

void primefiller()
{
    // method used to fill the primes set is seive of
    // eratosthenes(a method to collect prime numbers)
    vector<bool> seive(250, true);
    seive[0] = false;
    seive[1] = false;
    for (int i = 2; i < 250; i++) {
        for (int j = i * 2; j < 250; j += i) {
            seive[j] = false;
        }
    } // filling the prime numbers
    for (int i = 0; i < seive.size(); i++) {
        if (seive[i])
            prime.insert(i);
    }
}
// picking a random prime number and erasing that prime
// number from list because p!=q
int pickrandomprime()
{
    int k = rand() % prime.size();
    auto it = prime.begin();
    while (k--)
        it++;
    int ret = *it;
    prime.erase(it);
    return ret;
}
void setkeys()
{
    int prime1 = pickrandomprime(); // first prime number
    int prime2 = pickrandomprime(); // second prime number
    // to check the prime numbers selected
    // cout<<prime1<<" "<<prime2<<endl;
    n = prime1 * prime2;
    int fi = (prime1 - 1) * (prime2 - 1);
    int e = 2;
    while (1) {
        if (__gcd(e, fi) == 1)
            break;
        e++;
    } // d = (k*Φ(n) + 1) / e for some integer k
    public_key = e;
    int d = 2;
    while (1) {
        if ((d * e) % fi == 1)
            break;
        d++;
    }
    private_key = d;
}
// to encrypt the given number
long long int encrypt(double message)
{
    int e = public_key;
    long long int encrpyted_text = 1;
    while (e--) {
        encrpyted_text *= message;
        encrpyted_text %= n;
    }
    return encrpyted_text;
}
// to decrypt the given number
long long int decrypt(int encrpyted_text)
{
    int d = private_key;
    long long int decrypted = 1;
    while (d--) {
        decrypted *= encrpyted_text;
        decrypted %= n;
    }
    return decrypted;
}
// first converting each character to its ASCII value and
// then encoding it then decoding the number to get the
// ASCII and converting it to character
vector<int> encoder(string message)
{
    vector<int> form;
    // calling the encrypting function in encoding function
    for (auto& letter : message)
        form.push_back(encrypt((int)letter));
    return form;
}
c++ string operators getline
1个回答
0
投票

您在这些行中错误地使用了

getline()

for (getline(b2, UserData.Username) == LoginUsername)
... 
if(LoginCodedPassword == getline(b2,UserData.codedPassword))

getline()
返回对流的引用。您无法将其与字符串进行比较,因此会出现错误。
getline()
修改您在第二个参数中传递给它的字符串,您需要对其进行比较。

© www.soinside.com 2019 - 2024. All rights reserved.