This is the assignment I got: 3. Modify your password program from before to put all of the password checking logic into a
separate function, apart from the rest of the program.
Now when I execute the code what happens is it always gives me the Else statement even when I use the correct username and password but doesn't count the LoginAttempt. Now I suspect I messed up somehow with the function but I have no clue what I did wrong. So where did I mess up?
#include <iostream>
#include <string>
usingnamespace std;
void checkp()
{
string username;
string password;
int LoginAttempt = 0;
if ( username == "root" && password == "123" )
{
cout << "Acces Granted: " << "\n";
}
else
{
cout << "Bad password or username please try again" << "\n";
LoginAttempt ++;
}
}
int main()
{
string username;
string password;
int LoginAttempt = 0;
while ( LoginAttempt < 5 )
{
cout << "Please enter your username: " << "\n";
cin >> username;
cout << "Please enter your password: " << "\n";
cin >> password;
checkp();
}
cout << "Too many login attempts terminating the program" << "\n";
}
Now what I think is that the string username; and string password; in the checkp function are local variables so they never are correct right? How do I change it so the scope is available in int main() or is that not what has gone wrong?