Please please please try to avoid the use of
goto (It has its uses, but this is
not one of them).
1 2
|
std::string loginattempts=0;
for ( loginattempts != 3){
|
Okay, I would highly suggest reading the site's tutorial on flow control:
http://www.cplusplus.com/doc/tutorial/control/
See the for loop section. You're using a for loop as if it were a while loop.
Second, you have
loginattempts as a
string. It is not an integer. You cannot assign it the value of 3. Types matter a lot in C++.
If you don't mind, I'm just going to make a simpler version using a proper loop that you can hopefully learn from.
Also, in proper security UI, you should not give a message that differentiates whether they got the
username or the
password wrong. The less information for the attacker, the better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
int main()
{
const int login_attempts = 3;
std::string username;
for (int i = 0; i < login_attempts; i++)
{
std::cout << "Enter username: ";
getline(std::cin, username);
std::string password;
std::cout << "Enter password: ";
getline(std::cin, password);
if (username == "jono" && password == "jono")
{
break;
}
else
{
if (i < login_attempts - 1)
{
std::cout << "Login incorrect." << std::endl;
}
else
{
std::cout << "Locking out." << std::endl;
return 1;
}
}
}
std::cout << "Welcome, " << username << ".\n";
}
|
Also, don't mark your thread with a green check mark until
after you're satisfied with the answer :) I almost didn't reply because it looked like you had already figured it out or something.
PS: I now see mbozzi's post. It has good annotations to tell you what's happening in plain English, so I suggest reading those.