3 Log in attempts

I was trying to do a 3 login attempt system.
Whenever I try to run the code, it says there is no match for operator != .

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
 #include <iostream>
#include <string>
using namespace std;
int main()
{
    string username;
    string password;
    int a;
    int b;
    {
    cout << "Input username"<< endl;
    getline(cin, username, '\n');
    cout <<"Input password" << endl;
    getline(cin, password, '\n');

    while ( username&&password != a&&b <=2)
    {
        cout << "log in attempt no:"<< a+1;
        cout << "Enter username again" << endl;
        getline(cin, username, '\n');
        cout << "enter pass again"<< endl;
        getline(cin, password, '\n');
        a++;       
    }
if (username&&password == a&&b <=3)
    {
        cout << " log in verified"<< endl;
    }
    else{
        cout <<"3 attempts reached"<< endl;
    }
}
}
Last edited on
First, variables like 'a' and 'b' are poorly named. They don't tell me what they are supposed to be, at all.

16:32: error: no match for 'operator!=' (operand types are 'std::string {aka std::basic_string<char>}' and 'int')
You're trying to compare a string to an int for inequality. Explain in words what you want the condition on line 16 to be checking for, because it makes no sense as it stands now.

Also, just a general note: You should always enable compiler warnings. Your a and b variables are not initialized.
Last edited on

unorthodox03 (1)
I was trying to do a 3 login attempt system.
Whenever I try to run the code, it says there is no match for operator != .

#include <iostream>
#include <string>
using namespace std;
int main()
{
string username;
string password;
int a;
int b;
{
cout << "Input username"<< endl;
getline(cin, username, '\n');
cout <<"Input password" << endl;
getline(cin, password, '\n');

while ( username&&password != a&&b <=2)
{
cout << "log in attempt no:"<< a+1;
cout << "Enter username again" << endl;
getline(cin, username, '\n');
cout << "enter pass again"<< endl;
getline(cin, password, '\n');
a++;
}
if (username&&password == a&&b <=3)
{
cout << " log in verified"<< endl;
}
else{
cout <<"3 attempts reached"<< endl;
}
}
}


the if statements are nonsense: b is not initialized, and && on a pair of strings means 'what' to you?

I would think you want something like this:
1
2
3
4
5
6
7
8
do
{
  ask for user name
  get user name
  ask for password
  get password
  counter++;
} while (username != valid_user_name && password != valid_users_password && counter < 3)

**where you have a valid login /password to compare the input to.
if counter is good, proceed, else too many attempts, do whatever (exit program?) Note that after the loop counter could be 3 and be valid. its <4 after the loop, because it increments it regardless of good/bad input.
Last edited on
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>
using namespace std;
int main()
{
    string username;
    string username2 = "John Doe";
    string password;
    string password2 = "open dude";
    
    //int a;
    int b = 0;
    
    cout << "Input username"<< endl;
    getline(cin, username, '\n');
    cout <<"Input password" << endl;
    getline(cin, password, '\n');

    while ( username != username2 && password != password2 && b <2 )
    {
        cout << "log in attempt no:"<< b+1 << endl;
        cout << "Enter username again" << endl;
        getline(cin, username, '\n');
        cout << "enter pass again"<< endl;
        getline(cin, password, '\n');
        b++;       
    }
    
if (username == username2 && password == password2)
    {
        cout << " log in verified"<< endl;
    }
    else{
        cout <<"3 attempts reached"<< endl;
    }

}
Line 19: Don't you want || (logical or)?
As written, both username and password must be wrong in order to reprompt for them.
Yes you are right. Good catch!
Topic archived. No new replies allowed.