如何阅读多个文本行

时间:2015-01-28 08:37:33

标签: c++ text text-files lines

在我的文本文件(“UsernamePassword.txt”)中,有多行用户名和密码。当我尝试使用第二行或第三行上的用户名和密码登录时,它会将我带到“无效的用户名或密码”部分。只有第一行用户名和密码才有效。

有关如何阅读多行的任何建议吗?

{
    fstream inFile;
    string user, pass, username, password;
    int choice;
    Logo();
    cout << endl << endl << endl;
    inFile.open("UsernamePassword.txt");
    if (!inFile)
        cout << "Unable to Open File";
    else 
    {       
        while (username != user)
        cout << endl << endl << endl;
        cout << "               Please enter username: ";
        cin >> user;
        cout << "               Please enter password: ";
        cin >> pass;
        {
            inFile >> username >> password;

            if (user == username && pass == password)
            {
                system("cls");
                cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
                cout << "\t            ****************************************** " << endl;
                cout << "\t            **  !!!   Welcome to CherryLunch   !!!  ** " << endl;
                cout << "\t            ****************************************** " << endl;
                cout<<endl<<endl<<endl<<endl<<endl;
                system("pause");
                system("cls");

                MainMenu();

            }
            else
            {           
                cout<<endl<<endl<<endl<<endl<<endl;
                cout << "\t              !!!   Invalid Username or Password   !!!" << endl<<endl;
                cout << "\t                    ***   Please try again   ***" << endl;
                cout<<endl<<endl<<endl<<endl<<endl;

                system("pause");
                system("cls");
            }
        }
    }
    inFile.close(); 
}

1 个答案:

答案 0 :(得分:0)

您还没有向我们提供完整的代码 - 但我已在您的代码(各个地方)中发表评论,以表明您是愿意这样做还是错误地执行此操作:

{
fstream inFile;
string user, pass, username, password;
int choice;
Logo();
cout << endl << endl << endl;
inFile.open("UsernamePassword.txt");
if (!inFile)
    cout << "Unable to Open File";
else 
{       
    while (username != user)  // Only the following run will repeat ( if this is the intention?)
    cout << endl << endl << endl;
    cout << "               Please enter username: ";
    cin >> user;
    cout << "               Please enter password: ";
    cin >> pass;
    {  // This is scoped operation
        inFile >> username >> password;

        if (user == username && pass == password)
        {

            system("cls");
            cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            cout << "\t            ****************************************** " << endl;
            cout << "\t            **  !!!   Welcome to CherryLunch   !!!  ** " << endl;
            cout << "\t            ****************************************** " << endl;
            cout<<endl<<endl<<endl<<endl<<endl;
            system("pause");
            system("cls");

            MainMenu();

        }
        else
        {           
            cout<<endl<<endl<<endl<<endl<<endl;
            cout << "\t              !!!   Invalid Username or Password   !!!" << endl<<endl;
            cout << "\t                    ***   Please try again   ***" << endl;
            cout<<endl<<endl<<endl<<endl<<endl;

            system("pause");
            system("cls");
        } // End of inner else
    } // This is the end of scoped operation
}  // End of Outer else
inFile.close();