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.

Recommended Answers

All 3 Replies

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

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

you could also use cin.get() to get rid of the newline.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.