如何停止 while 循环
How to stop a while loop
这个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;
失败,username
和password
将包含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...
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 输入循环 while 函数
- c++中的循环(while)
- 为什么这段代码没有循环(while)