Creating a password prompt

Hi, i'm creating a password prompt and i'm having errors. I want to create something that only allows 3 password attempts.

This is what i've come to so far..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main()

{
    string password;

    cout << "Please enter password";
    cin >> password;

    for ( int password != "freddycakes"; password < 4; password + 1; )
    {
        cout << " You have entered a wrong password" << '\n';
    }

    else
    {
        cout << " You entered a correct password" << '\n';
    }

}
Last edited on
You need to read through the C++ tutorial again, in particular the flow control chapter:
http://www.cplusplus.com/doc/tutorial/control/

The for() loop you have written does not make much sense.

Its "fields" are incorrect (you put a declaration-condition thing in the initialization field), and you put a useless operation in the increase field (useless because it doesn't really increment password).

Then you add an else branch to your for() loop. The else branch is for if()'s only. You cannot add it to anything else.

1
2
3
4
5
6
if (/* condition */)
{
}
else
{
}


I could rewrite the program so that it runs as intended, but you are very close to figure it out yourself anyway, and by helping you that way I'd prevent you from learning.

You just need to read the article I linked you to, and try again.

Edit: typos.
Last edited on
Trying to enjoy this learning process.. so frustrating though lol.

Getting errors on line 24.

Am I getting closer?


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 password;

    string i = password;

    cout << "Please enter password" << '\n';
    cin >> password;

    if ( password == "freddycakes" )

    {
        cout << "Correct passoword" << '\n';
    }

    for ( string i != "freddycakes"; i < 4; i + 1)

    {
        cout << "Incorrect password" << '\n';
    }


}




Last edited on
You should check out the tutorial about the for loop again http://www.cplusplus.com/doc/tutorial/control/

In your example the maximum number of retires is 3.
This should be the initialization part of the for loop (like a counter) Now each time the user enters a wrong passwod the for loop will execute again and decrement your initialization value until it it reaches 0.
Now write the above in code and you have your for loop.


Style suggestion: do not leave so many empty lines. They waste vertical space and some people may find them annoying.

You must remember why you are using a for() loop.

You are using a for() loop because you want to limit how many times you read the password. You want to make sure that the user gets three tries, and no more.

So the obvious first thing to do is to move the reading inside the for() loop.

1
2
3
4
5
for (/* ??? */)
{
    cout << "Enter your password: ";
    cin >> password;
}


The next thing to do is to move the password check inside the for() loop as well, right after you read it. Because obviously, every time you read a new password you must also check it.

1
2
3
4
5
6
7
8
9
10
for (/* ??? */)
{
    cout << "Enter your password: ";
    cin >> password;

    if (password == "freddycakes")
    {
        /* ??? */
    }
}


Now we must think about what to write instead of the /* ??? */ comments.

The one in the if() is a bit trickier, so I'll help with that. The other one, again you should be able to figure out on your own.

So what if the password was guessed correctly? Then it means we no longer need to read new passwords to check. We must stop the for() loop.

This is done by using the break; instruction.

1
2
3
4
5
6
7
8
9
10
11
for (/* ??? */)
{
    cout << "Enter your password: ";
    cin >> password;

    if (password == "freddycakes")
    {
        /* ??? */
        break;
    }
}


There's one more thing, we have to remember if the password was guessed correctly or not.

For this we use a bool flag named password_was_guessed. After the for() loop we check this flag. If it is true, then we print "Correct password", otherwise we print "Incorrect password".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool password_was_guessed = false;

for (/* ??? */)
{
    cout << "Enter your password: ";
    cin >> password;

    if (password == "freddycakes")
    {
        password_was_guessed = true;
        break;
    }
}

if (password_was_guessed == true)
{
    cout << "Correct password\n";
}
else
{
    cout << Incorrect password\n";
} 

Okay. I'm at a stage where i can enter a password WITHOUT spaces and have 3 attempts.

But when i enter a password with one space ("hi there") for the first attempt and no spaces for the second attempt.
It only gives me 2 attempts, then it terminates.

Plus when i enter a password with a space. It prints "Enter your password" twice.

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

using namespace std;

int main()
{

string password;
int i;
int password_attempts;
bool password_was_guessed = false;

for ( int i = 0; i < 3; ++i  )
{
    cout << "Enter your password: ";
    cin >> password;

    if (password == "freddycakes")
    {
        password_was_guessed = true;
        break;
    }
}

if (password_was_guessed == true)
{
    cout << "Correct password\n";
}
else
{
    cout << "Incorrect passowrd\n";
}
}
Last edited on
Space is considered a delimiter.

So when you give a password that contains a space such as "hi there" it gets broken up into "hi" and "there". So it's as if you give two passwords at the same time. After "hi" is checked, "there" follows automatically.

If you want to read a sentence as a password, you use std::getline() to read.
By default, the std::getline() considers the delimiter to be newline (Enter key).

1
2
3
//    cin >> password; // replace with

getline(cin, password);


http://www.cplusplus.com/reference/string/string/getline/
Topic archived. No new replies allowed.