Need help with Function for password program

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?

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
38
39
  #include <iostream>
#include <string>

using namespace 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?
Last edited on
You need to pass the variables to the function. I will give a code example in a minute.
Topic archived. No new replies allowed.