Error when adding a for loop to limit password entry tries

Hi again, I'm trying now to add a 3 times limit of the number of times a person can type in the wrong password into the console.

I did write this code before the other day but I wanted to use a bool function too this time and combine them.

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
#include <iostream>
#include <strings>
using namespace std;
bool green (string password) // error here saying "'bool green' redeclared as different kind of symbol"
{
    return password == "google";
}
int main()
{
    string password;// defining string in the main function not the do function

    for (int tries = 4; tries <= 3; --tries) //if pass is 3 or less, take away 1 try and do..
    { // .. this stuff here
            cout << "whats ur password? \n" ;
            getline (cin, password, '\n');

    if (green(password)) // if pass is green
        {
            cout << "Go on through!" << '\n'; // cout and
            break; // break the tries 'for loop' too
        }


    else
        {
            cout << "Wrong password" << endl;
            return 0; // after 3 tries, close it.
        }
    }
return 0;
}

The reason why I posted this is because It just shows up empty when I build and run it build log no errors.. Maybe I'm trying to do too much at a time? or is there a problem with the source code.

Thank you for your time
Last edited on
couple of things wrong for (int tries = 4; tries <= 3; --tries) is the first look at the end condition. The code inside the loop will never get executed.

second your are returning from else, so when you change the end condition of the loop, program is going to end after first wrong password
Thanks for your help dude, It's perfect now :D
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
#include <iostream>
#include <string>
using namespace std;
bool green (string password)
{
    return password == "google";
}
bool red (string password)
{
    return password != "google";
}
int main()
{
    string password;// define string in the main function not the do function

    for (int tries = 3; tries <= 3; --tries) //if pass is 3 or less, take away 1 try and do..
    { // .. this stuff here
            cout << "whats ur password? \n" ;
            getline (cin, password, '\n');

    if (green(password)) // if pass is green
        {
            cout << "Go on through!" << '\n'; // cout
            break;
        }

    if (tries == 0) // check if tries run out first
        {
            cout << "You've ran out of tries now.." << endl;
            return 0; // after 3 tries, close it.
        }

    else if (red(password)) // then check if the pass is incorrect
    {
        cout << "wrong password, Try again \n" ;
        cout << "----------------------------------------";
        cout << '\n';
    }

    }
return 0;
}
Last edited on
Topic archived. No new replies allowed.