0
string name;
char gender;
string address;

cout << "Enter the name : ";
getline(cin,name);
cout << "Enter the gender (M/F) : ";
cin >> gender;
cout << "Enter the address : ";
getline(cin,address);

I don't understand why this skips the input action for the address.

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by NathanOliver
0

Because there is a newline left in the buffer from the call to cin >> gender . You will need to get it out before you call getline again. Narue has a good post about there here

Edited by NathanOliver: n/a

0

Thanks for the reply. So would this be the best way to solve it in this example?

string name;
char gender;
string address;

cout << "Enter the name : ";
getline(cin,name);
cout << "Enter the gender (M/F) : ";
cin >> gender;
cin.ignore();
cout << "Enter the address : ";
getline(cin,address);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.