using While loops to validcate string input

Hi Everyone, Im having trouble with the while loop, this program should ask the user for their name, if incorrect it should send an error msg and ask for input again...after the correct name is entered the program should prompt the user to enter a password, again if incorrect it should send an error msg and ask for input again.

the code below does what it should when asking for a name..HOWEVER it does not allow the user to enter a password and instead jumps straight to the error msg..
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
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

int main()
{
// Variables
string username;				
string password;				

//Intro: Welcome User
cout << " Hello and welcome to the prestigious Bank of Kwantlen" << endl;
cout << " Enter your name: ";
cin >> username;

while ((username != "sadia") && (username != "rajput"))
{
    cout << " Sorry that is incorrect. Please enter your name:";
    cin >> username;
}

cout << " Welcome, Sadia Rajput." << endl;
cout << " To continue, enter your password: "; 
 
cin >> password;


while (password != "yaycplusplus")
{
    cout << "That is incorrect, enter password: ";
    cin >> password;
}

cout << "You have been successfully authenticated. You are accessing membership number 100247817" << endl;

system("pause");
return 0;
}



after the correct name is entered, the program displays this:

"Welcome, Sadia Rajput."
"To continue, enter your password: That is incorrect, enter password: "


Any help is greatly appreciated, i would prefer to do this using the while loops and not bool statements
Last edited on
See my answer in section for beginners.
Topic archived. No new replies allowed.