IF and ELSE statements within a WHILE statement help?

Okay so I i'm just messing around with some code trying to learn on my own.

Anyways, I'm trying to do like a mock password thing just using strings and integers.

I have it setup so that it asks for what you want your "password" to be, and then it will ask for the password again. I want it to say "Great" when it was right, and "Wrong Try Again!" is it is wrong, but for some reason when I type in the password correctly, it will still do the else statement and the output would be

Welcome,
What would you like your password to be?
<insert password>
What was your password?
<insert previously inserted password>
Great!
Wrong Try Again!

What can I do to keep the statement from falling through?

This is my code:
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>
#include <string>

using namespace std;

int main(){

    string password;
    string newpassword;
    int b = 2;

    cout << "Welcome," << endl << "What would you like your password to be?" << endl;
    cin >> newpassword;

    while(b!=1){
        cout << "What was your password?" << endl;
        cin >> password;
        if(password==newpassword){
            cout << "Great!" << endl;
            b = 1;
        }
        else(password!=newpassword);{
            cout << "Wrong Try Again!";
    }
    }

return 0;
}
Hi,
1
2
3
else(password!=newpassword);{
            cout << "Wrong Try Again!";
    }


==>
1
2
3
else if (password != newpassword){
            cout << "Wrong Try Again!";
    }


"else" should be "else if"
Does that help? :)
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>
#include <string>

using namespace std;

int main(){

    string password;
    string newpassword;
    int b = 2;

    cout << "Welcome," << endl << "What would you like your password to be?" << endl;
    cin >> newpassword;

    while(b!=1){
        cout << "What was your password?" << endl;
        cin >> password;
        if(password==newpassword){
            cout << "Great!" << endl;
            b = 1;
        }
        else if (password!=newpassword);{
            cout << "Wrong Try Again!";
        }
    }

return 0;
}


This is the new code with the addition of the ELSE IF statement and it still has the same output as before.
> This is the new code with the addition of the ELSE IF statement and it still has the same output as before.

That is just what I have wanted you to say, bro. Have you noticed another small difference in my code correction?

It says :
"Do not put a semi-colon ( ; ) after an if-statement or an else if-statement".
Does that help you? : )
Yes thank you.
Good to hear :)
Topic archived. No new replies allowed.