[英]Login System C++: After entering my string User & Pass, It asks me to re-input them
My code is a Shop that I am trying to build, it works(Register system working) until I input my User and Password(Login System), after inputting my Username, the program asks me to re-login and it keeps doing it(Pretty sure it is because it is a while true loop). 我的代码是我要建立的商店,它可以正常运行(注册系统),直到输入用户名和密码(登录系统)为止。输入用户名后,程序会要求我重新登录并继续进行操作(可以肯定是因为这是一会儿真实循环)。 (This is a project, so there are separate different files with constructors and classes.)
(这是一个项目,因此存在带有构造函数和类的单独的不同文件。)
Here is part of my Code: 这是我的代码的一部分:
while(true)
{
cout << "Would you like to register or login?" << endl;
string answer = "";
cin >> answer;
if(answer == "register" || answer == "Register")
{
cout << "What would be your designated username?: " << endl;
string newUser;
cin >> newUser;
for(int i = 0; i < 20; i++)
{
if(customers[i] -> username != newUser)
{
cout << "what would be your designated password?: " << endl;
string newPass;
cin >> newPass;
customers[lastRegisteredID] = new Customer(newUser, newPass);
lastRegisteredID++;
break;
}
}
//^Register Part.
}
if(answer == "login" || answer == "Login")
{
cout << "Your username: " << endl;
string UserAttempt;
cin >> UserAttempt;
for(int j = 0; j < 20; j++)
{
if(customers[j] -> username == UserAttempt)
{
cout << "Username Found!" << endl;
tempCustomer = customers[j];
cout << "Your password: " << endl;
string PassAtempt;
cin >> PassAtempt;
if(tempCustomer -> password == PassAtempt)
{
cout << "Password correct \n Successfully logged in." << endl;
loggedin = true;
break;
}
}
}
}
//^Login part.
}
The problem you're asking about comes (as the comments of your question already said) from the fact, that your break;
您要问的问题(正如您对问题的评论所说)来自事实,您
break;
only get's you out of the for loop (the innermost loop), not the while loop. 只会使您脱离for循环(最里面的循环),而不会进入while循环。 The easiest way to fix this would be to replace your
while(true)
with while(!loggedin)
. 解决此问题的最简单方法是将
while(true)
替换为while(!loggedin)
。 Notice also, that there are some other problems in the code you've posted. 还要注意,您发布的代码中还有其他一些问题。 As I don't know if these bugs are also in your final code, I will only list them (the few I've found):
因为我不知道这些错误是否也在您的最终代码中,所以我只会列出它们(我发现的几个):
You are not breaking out of your while
loop after performing a successful login
operation. 执行成功的
login
操作后,您不会脱离while
循环。
Also, your code crashes if fewer than 20 customers have registered. 另外,如果注册的客户少于20个,您的代码也会崩溃。
Also, your code allows multiple users to register using the same username and even the same password. 另外,您的代码允许多个用户使用相同的用户名甚至相同的密码进行注册。
Try something more like this instead: 尝试类似这样的方法:
Customer* customers[20];
int numCustomers = 0;
bool loggedin = false;
Customer* findCustomer(const std::string &user)
{
for(int i = 0; i < numCustomers; ++i)
{
if (customers[i]->username == user)
return customers[i];
}
return NULL;
}
...
while (true)
{
std::cout << "Would you like to register or login?" << std::endl;
std::string answer;
std::cin >> answer;
std::transform(answer.begin(), answer.end(), ::tolower);
if (answer == "register")
{
std::cout << "What would be your designated username?: " << std::endl;
string newUser;
std::cin >> newUser;
Customer *cust = findCustomer(newUser);
if (cust)
{
std::cout << "That username is already taken!" << endl;
continue;
}
if (numCustomers >= 20)
{
std::cout << "Too many users are registered!" << endl;
continue;
}
std::cout << "what would be your designated password?: " << std::endl;
std::string newPass;
std::cin >> newPass;
customers[numCustomers] = new Customer(newUser, newPass);
++numCustomers;
continue;
}
if (answer == "login")
{
std::cout << "Your username: " << std::endl;
std::string UserAttempt;
std::cin >> UserAttempt;
std::cout << "Your password: " << std::endl;
std::string PassAttempt;
std::cin >> PassAttempt;
Customer *cust = findCustomer(UserAttempt);
if ((cust) && (cust->password == PassAttempt))
{
std::cout << "Successfully logged in" << std::endl;
loggedin = true;
break;
}
std::cout << "Not logged in!" << std::endl;
continue;
}
std::cout << "Unknown command! Try again" << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.