Login program with warning :(

Guys Im trying to make a login program wherein there will be a warning in each attempt and I only have to do 3 attempts. In this program I made no matter what condition I put below it seemed to always put Unsuccesful login 2 attempts more even if the attempt is only down to 1 which is supposed to display Unsuccesful login 1 attempt more ugh help pls :(

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
  #include <iostream>
using namespace std;
int main()
{
    string username, password;
    for (int attempts = 0; attempts <= 2; attempts++)
    {
        cout << "Enter your username:" << endl;
        cin >> username;
        cout << "Enter your password:" << endl;
        cin >> password;
        if (username == "user" && password == "pass")
        {
            cout << "Successful login." << endl;
            return 0; // prevent from looping again
        }
        else if (attempts <=2){
		
            cout << "Unsuccessful login 2 attempts more." << endl;}
       else if (attempts <=1){
	   
        	cout <<"Unsuccesful login 1 attempt more. "  <<endl;
		}
    }
    cout << "Maximum attempts reached." << endl;
    system ("PAUSE");
	return 0;
}
it seemed to always put Unsuccesful login 2 attempts more even if the attempt is only down to 1 which is supposed to display Unsuccesful login 1 attempt more


else if (attempts <=2){
When attempts = 1, this statement will be true and will be executed, hence outputting "Unsuccessful login 2 attempts more."
1
2
3
4
else if (attempts <=1){
	   
        	cout <<"Unsuccesful login 1 attempt more. "  <<endl;
		}

As a result, this statement won't be executed, due to the nature of else if.

I think a more logical approach would be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int max_attempts = 3;
string username, password;
for(int attempts = 0; attempts < max_attempts; attempts++)
{
    // ...
    if (username == "user" && password == "pass")
    {
        cout << "Successful login." << endl;
        return 0; // prevent from looping again
    }
    else{
        cout << "Unsuccessful login " << max_attempts - attempts - 1 << " attempts more.\n";
    }
}
Last edited on
Uhmm basically it should show 3 attempts when the username and password are invalid. Hence in each attempt there must also be a corresponding warning. I can't get the matrix sir :( Please elaborate it for me I am an extreme beginner!
I realised I made a silly mistake in the code above. Its fixed now.

Uhmm basically it should show 3 attempts when the username and password are invalid. Hence in each attempt there must also be a corresponding warning.
Enter your username:
a
Enter your password:
a
Unsuccessful login 2 attempts more.
Enter your username:
s
Enter your password:
s
Unsuccessful login 1 attempts more.
Enter your username:
d
Enter your password:
d
Unsuccessful login 0 attempts more.
Maximum attempts reached.

Is this not what you want? Please elaborate.
Thats basically what I need sir
How should I run it sir. I can't run it in the console can you please show me the full code sir sorry im so noob
Oh sir integralfix thank you for your help I am now able to run the program thank you very muchhhhhh <3 <3 God bless
Topic archived. No new replies allowed.