如何停止while循环

时间:2015-01-28 05:38:41

标签: c++ while-loop fstream

这个while循环永远不会结束。例如,当我输入错误的密码时,它会继续输入错误的密码"一遍又一遍。

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

    while (username != user)
    {
        inFile >> username >> password;

        if (user == username && pass == password)
        {
            cout << endl;
            cout << "Welcome to CherryLunch!" << endl;

            system("pause");
            system("cls");

            MainMenu();

        }
        else
        {           
            cout << endl;
            cout << "           Invalid Username or Password!" << endl << endl;
            system("pause");
            system("cls");
        }
    }
}
inFile.close(); 

5 个答案:

答案 0 :(得分:1)

while循环是无限的,因为您永远不允许用户输入新密码或用户名。当if语句失败时,它将返回循环标题(它仍然是错误的)并继续向前。

让用户有机会输入新的用户/通过组合,然后循环仍然是有限的(前提是用户最终提供正确的凭据)。

答案 1 :(得分:0)

在while循环中移动cout和cin语句:

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)
        {
            cout << endl;
            cout << "Welcome to CherryLunch!" << endl;

            system("pause");
            system("cls");

            MainMenu();

        }
        else
        {           
            cout << endl;
            cout << "           Invalid Username or Password!" << endl << endl;
            system("pause");
            system("cls");
        }
    }
}
inFile.close(); 

答案 2 :(得分:0)

它保持无限循环,因为你永远不会问你是否到达了文件的末尾,因此,如果文件中没有包含与用户输入的对匹配的username/password组合,那么到达文件,行:

inFile >> username >> password; 

失败,usernamepassword将包含UsernamePassword.txt上显示的最后一个条目,循环将永远存在。

您的程序的以下实现将测试eof文件对象中的inFile

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
int main() { 
    std::ifstream inFile;    

    std::string user, pass;
    std::string username, password;

    inFile.open("UsernamePassword.txt");
    if (!inFile) {
        std::cout << "Unable to Open File";
    } else {
        std::cout << std::endl << std::endl << std::endl;
        std::cout << "           Please enter username: ";
        std::cin >> user;
        std::cout << "           Please enter password: ";
        std::cin >> pass;

        while (username != user && !inFile.eof()) {
            inFile >> username >> password;

            if (user == username && pass == password) {
                std::cout << std::endl;
                std::cout << "Welcome to CherryLunch!" << std::endl;

                // Equivalent to the 'pause' command in linux
                system("read -p 'Press any key to continue...' key");
                // Equivalent to the 'cls' command in linux
                system("clear");

                // MainMenu();
            } else {           
                std::cout << std::endl;
                std::cout << "           Invalid Username or Password!" << std::endl << std::endl;

                system("read -p 'Press any key to continue...' key");
                system("clear");
            }
        }
    }
    inFile.close(); 
}

答案 3 :(得分:0)

您正在尝试检查用户的用户名和密码,并与文件中的所有用户名和密码进行比较。
你的方法似乎很好。但是,您在每次比较后打印“无效的用户名...”,而不是比较文件中的所有用户名。因此将此输出移到else块之外并将其置于while循环中。

infile分别检查每一行。

并检查文件的结尾。如果在文件末尾找不到用户名和密码,则打印“Invald username”并提供用户输入另一组名称和密码

希望这会有所帮助!!

答案 4 :(得分:0)

首先,从文件中读取正确的用户名和密码,并将其保存在内存中。这样,如果用户名/密码验证第一次失败,则在检查用户输入的下一个用户名/密码之前,您不必重新打开或寻找开头的文件.... / p>

std :: map usernames_passwords;     std :: string username,password;

if (ifstream in("UsernamePassword.txt"))
    while (in >> username >> password)
        usernames_passwords[username] = password;
else
{
    std::cerr << "Unable to open username/password file\n";
    exit(EXIT_FAILURE);
}

然后提示登录详细信息,直到它们有效:

bool logged_in = false;
while (!logged_in &&
       std::cout << "\n\n\n    Please enter username: " &&
       std::cin >> username &&
       std::cout << "    Please enter password: " &&
       std::cin >> password)
{
    // look for a match in the earlier-read login details...
    auto it = usernames_passwords.find(username);
    logged_in = it != std::end(usernames_passwords) && it->second == password;
}

// the while loop could also exit because "cin >>" failed, indicating EOF
// that could be because the user typed e.g. ^D (UNIX) or ^Z (Windows), or
// input was from a redirected file or pipe or over a network connection...

if (!logged_in)
{
    std::cerr << "error reading login details from stdin\n";
    exit(EXIT_FAILURE);
}

...ok - we know the username/password are good - do whatever else...