如何停止while循环 [英] How to stop a while loop

查看:1757
本文介绍了如何停止while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个while循环永不结束。例如,当我输入错误的密码时,它会一直重复到不正确的密码部分。

This while loop never ends. For example, when i enter a wrong password it will keep on going to the "incorrect password" part over and over again.

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语句失败时,它将返回到循环标题(它仍然是错误的),并继续向前。

The while loop is infinite because you never allow the user to input a new password or username. When the if statement fails, it will return to loop header (where it will still be wrong) and continue onwards.

给用户一个机会输入一个新用户/ pass combo,然后循环仍然可以是有限的(如果用户最终提供了正确的凭据)。

Give the user a chance to enter in a new user/pass combo and then the loop can still be finite (provided the user eventually provides the correct credentials).

这篇关于如何停止while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆