C++ 为什么我总是在无限循环中结束?
即使是在do while循环中,我也会一直在一个无限循环中结束 我做错了什么?我什么都试过了,但还是想不出来。有什么帮助吗 这是我的密码:C++ 为什么我总是在无限循环中结束?,c++,loops,infinite-loop,C++,Loops,Infinite Loop,即使是在do while循环中,我也会一直在一个无限循环中结束 我做错了什么?我什么都试过了,但还是想不出来。有什么帮助吗 这是我的密码: #include <iostream> #include <string> #include <Windows.h> using namespace std; //function prototypes //prototype for IsAccessible int IsAccessible(string usern
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
//function prototypes
//prototype for IsAccessible
int IsAccessible(string username, string password);
//prototype for menu
void menu();
int main()
{
string username;
string password;
//make user to login
cout << "Enter username : ";
getline(cin, username);
cout << "\nEnter Password : ";
cin >> password;
IsAccessible(username, password);
cout << "Thank you for logging in!!";
cin.ignore();
cin.get();
return 0;
}
//function definitions
//definition for IsAccesible
int IsAccessible(string username, string password)
{
//check if user entered correct details
do
{
int x = 0;
if(password == "123" && username == "asdqw")
{
cout << "\n\nThank you for loggin in John!";
break;
}
//if user entered wrong details
else if(password != "123" && username != "asdqw")
{
cout << "\nYou have either entered a wrong password or username.\n";
cout << "Please retry.";
}
//if user exceeds limitation
if(x == 5)
{
cout << "\n\nYou have exceeded the 5 retry limitations......\n";
Sleep(4000);
cout << "Exiting program....";
Sleep(5000);
return 0;
}
}while(password != "123" && username != "asdqw");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
//功能原型
//可访问的原型
int可访问(字符串用户名、字符串密码);
//菜单原型
无效菜单();
int main()
{
字符串用户名;
字符串密码;
//让用户登录
密码;
i可访问(用户名、密码);
coutwhile
将继续循环,直到用户名不是“asqdf”,密码不是“123”,并且代码从不要求新的用户名和密码,因此它将继续循环到无穷大。此外,您不会在每次循环迭代时递增x
,因此最多5次尝试的代码将永远不会运行
最后一个提示-如果你的方法不需要返回,你可以将返回类型设置为void。你可以使用
break
语句,而不是返回退出do-while。while循环中的输入在哪里?如果你想终止它,你必须在循环中执行'cin>>password;'。我已经修复了它。为什么你认为do-while
天生不受无限循环的影响吗?