Limited Login Attempts

Hello c++ programmers,
Im a beginner in c++ and im trying to make a programm that will close after you get the password and or username wrong 3 or more times, down below is what im capable of but i know it needs improvements, please help on how to do this.

Thanks guys.




#include <iostream>
#include <string>

Int main()
{
Std::string loginattempts=0;
for ( loginattempts != 3){
std::cout<<"too much login attempts, programme will now close";
return 1;
}
l1:std:string username;
Std:: string password;
Std:cout<<"Enter username: ";
getline(cin, username);
If ( username == "jono"){
Std::cout<<"Correct!";
}
else{
std::cout<<"wrong!"<<std::endl;
loginattempts++;
std::cout<<"Login attempts is "<<loginattempts<<std::endl;
goto l1;
}
l2:Std::cout<<"enter password: ";
getline(cin, password);
if(password == "jono"){
Std::cout<<"correct";
}
else{
Std::cout<<"wrong!"<<std::endl;
loginattempts++;
std::cout<<"login attempts is "<<loginattempts;
goto l2;
}
return 0;
}
Last edited on
- You will find it very helpful to write your code in tiny pieces. Instead of writing the entire program and then attempting to fix problems once you're done, fix problems bit-by-bit. Do this by writing only a few lines of code and then compiling.
- When the compiler emits multiple errors, fix only one (the first) at a time. Attempt a fix, then re-compile. Do this until the program compiles cleanly.

- C++ is case-sensitive. You can't write Int instead of int.
- If you're going to label your statements at all, use names that describe where in the code the label refers. l1, l2 offers absolutely no information to a reader.

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
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

int main() {
  int const max_login_attempts = 3;
  std::string const correct_username = "jono";
  std::string const correct_password = "jono";

  // So far, the user has made no login attempts.
  int login_attempts_made = 0;
  while (login_attempts_made < max_login_attempts) {
    // prompt the user for a username.
    std::string username;
    std::cout << "Username: ";
    std::getline(std::cin, username);

    // prompt the user for a password.
    std::string password;
    std::cout << "Password: ";
    std::getline(std::cin, password);

    // user has made a login attempt
    ++login_attempts_made;

    // if the user got the username and password correct
    if (username == correct_username &&
        password == correct_password) {
      // exit the loop now.
      break;
    }
    // otherwise complain and continue:
    else {
      std::cout << "Invalid username or password.\n";
    }
  }

  // If we've made more login attempts than allowed:
  if (login_attempts_made >= max_login_attempts) {
    std::cout << "Too many login attempts.  Please try again later.\n";
    // return from the main function, ending the program.
    return 0;
  }
  // Otherwise, we must have got the username and password correct:
  else {
    std::cout << "Login successful.\n";
  }
}

Last edited on
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.
Last edited on
@mbozzi - Wow! Thanks sir, that was really helpful, Im definitley going to take your advice and start writing my code bit by bit then complie. Thank you so muchπŸ‘πŸ‘πŸ‘

@Ganado - Really good feedback, thanks sirπŸ‘
Topic archived. No new replies allowed.