【问题标题】:How to read multiple text lines [duplicate]如何阅读多行文本[重复]
【发布时间】:2015-01-28 08:37:33
【问题描述】:

在我的文本文件(“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(); 
}

【问题讨论】:

  • 您是否将 while 语句放在了错误的行上?此外,显示您的文本文件的 sn-p。
  • while (username != user) 移动到cin &gt;&gt; pass; 下方的一行我认为这可能是您打算做的。但这并不是唯一的错误。
  • 这只是your other question here 中涉及的相同编码错误的另一个症状。那里的好答案也适用于此。

标签: c++ text text-files lines


【解决方案1】:

你没有给我们完整的代码 - 但我已经在你的代码中(各个地方)放置了 cmets,以表明你是自愿这样做还是错误地这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多