简体   繁体   中英

check username and password input with data stored in text file in c++

This is my code:

`
void Customer::validate_cust_username_and_password()
    {
        string uname, pword;
        cout << "enter name: " << endl;
        cin >> uname;
        cout << "enter password: " << endl;
        cin >> pword;
        ifstream myfile("cust_username_and_password.txt");
        if (myfile.is_open())
        {
            while (!myfile.eof())
            {

                if (uname == cust_username && pword == cust_password)
                {
                    cout << "Login successfully." << endl;
                    cust_mainmenu();
                    break;
                }
                else
                {
                    cout << "Wrong username or password!" << endl;
                    break;
                }
            }

            myfile.close();
        }

    }
`  

This is another code which store the username and password:

  `void Customer::cust_register_name_and_password()
{
    string un, pw;
    ofstream myfile("cust_username_and_password.txt", ios::out | ios::app);
    cout << "Enter Customer Username= " << endl;
    getline(cin, un);
    cout << "Enter Customer Password= " << endl;
    getline(cin, pw);
    myfile << endl << un << "  " << pw << endl;
    myfile.close();

    cout << "Register Successfully." << endl;
    system("pause");
}`

So the problem is when I enter the username and password which I already stored in the text file before, the output is only showing the "Wrong username or password!".

Really appreciate if anyone can help.

In your validate_cust_username_and_password function, you never read in the username and password. Add this:

 myfile >> cust_username >> cust_password;

在此代码中的任何位置都不会设置cust_usernamecust_password ,因此它们永远不会与用户的输入匹配(除非用户输入空字符串)。

    string uname, pword;
    cout << "enter name: " << endl;
    cin >> uname;
    cout << "enter password: " << endl;
    cin >> pword;
    ifstream myfile("password.txt");
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {

            if (uname == "test" && pword == "0000")
            {
                cout << "Login successfully." << endl;
                cust_mainmenu();
                break;
            }
            else
            {
                cout << "Wrong username or password!" << endl;
                break;
            }
        }

        myfile.close();
    }

}

Where did cust_username magically appear. Was Paul Daniels involved. Paul I take that back you are not as bad as David Blaine - but not a lot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM