簡體   English   中英

使用存儲在C ++文本文件中的數據檢查用戶名和密碼輸入

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

這是我的代碼:

`
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();
        }

    }
`  

這是另一個存儲用戶名和密碼的代碼:

  `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");
}`

所以問題是當我輸入之前已經存儲在文本文件中的用戶名和密碼時,輸出僅顯示“錯誤的用戶名或密碼!”。

非常感謝任何人都可以提供幫助。

validate_cust_username_and_password函數中,您永遠不會讀入用戶名和密碼。 添加:

 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();
    }

}

cust_username神奇地出現在哪里。 保羅·丹尼爾斯(Paul Daniels)是否參與其中。 保羅,我收回了你,不如大衛·布萊恩(David Blaine)糟糕-但不是很多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM