如何停止 while 循环

How to stop a while loop

本文关键字:循环 while 何停止      更新时间:2023-10-16

这个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(); 

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

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

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

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

它保持无限循环,因为您从不询问是否到达文件末尾,因此,如果文件不包含与用户输入的对匹配的username/password组合,则当到达文件末尾时,该行:

inFile >> username >> password; 

失败,usernamepassword将包含UsernamePassword.txt上看到的最后条目,循环将永远存在。

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

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

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

infile 单独检查每一行。

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

希望这有帮助!!

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

标准::地图usernames_passwords; 标准::字符串用户名、密码;

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

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

bool logged_in = false;
while (!logged_in &&
       std::cout << "nnn    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 stdinn";
    exit(EXIT_FAILURE);
}
...ok - we know the username/password are good - do whatever else...