(Beginner) string variables not equaling in if statement

Jan 31, 2017 at 4:20pm
I'm new to learning C++ and following a book.
I can't seem to figure out why I keep receiving the cout from the else statement instead of from the if statement.


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

using namespace std;

int main()
{
    string my_pass;
    string entered_pass;

    cout << "Please choose a password for yourself: ";
    cin >> my_pass;
    getline(cin, my_pass, '\n');
    cout << "Okay, now please verify that password: ";
    getline(cin, entered_pass, '\n');

    if (my_pass == entered_pass)
    {
        cout << "Great, password verified!";
    }
    else
    {
        cout << "Ahh can't be helped. I don't know how to let you enter another password...";
    }
}


When I change if (my_pass == entered_pass) to if (my_pass == "xyzzy"), and I enter "xyzzy" for my_pass, I still receive the cout from the else statement.

I've tried to look around on this forum and googled places, but either I haven't found it, or I don't understand what I'm reading.
Jan 31, 2017 at 4:28pm
Did you try to print out the "password" after you try to retrieve the information?

Why are you trying to have the use enter the password three times?

1
2
3
4
5
    cout << "Please choose a password for yourself: ";
    cin >> my_pass;   ///////// 1
    getline(cin, my_pass, '\n'); ///////// 2
    cout << "Okay, now please verify that password: ";
    getline(cin, entered_pass, '\n');  /////// 3 


By the way switching between the extraction operator>> and getline() is problematic because the extraction operator leaves the new line character in the input stream which causes getline() to terminate it's entry.


Jan 31, 2017 at 4:32pm
In this case, presumably the password doesn't contain spaces?
If that's the case, then use
 
    cin >> my_pass;

instead of
 
    getline(cin, my_pass, '\n');


Well - your code has both, and that's the cause of the problem. Stick to one or the other and it should work.

What happens is, after the cin >> at line 12, there is a trailing newline left in the input buffer. Then, when the getline() at line 13 is reached, the function will read into the string until a newline is found, resulting in an empty string.

On some occasions (not here) you do need to have both cin >> something and getline(something else). If you did need to do that, then you'd need to empty the input buffer after the cin >> statement.

One way to do that would be
 
    cin >> my_pass >> ws;

the ws means read and discard any whitespace, which will include the unwanted newline.

Another way is to use ignore, for example
1
2
    cin >> my_pass;
    cin.ignore(1000, '\n'); // ignore up to 1000 characters, or until a newline is found 



Jan 31, 2017 at 4:43pm
Thank-you both very much! I took out
 
cin >> my_pass;

and it worked! I hadn't realized what the getline() was really doing until now.
Topic archived. No new replies allowed.