Possible Duplicate:
getline not asking for input?
There is some unique thing happening in my program. Here are some set of commands :
cout << "Enter the full name of student: "; // cin name
getline( cin , fullName );
cout << "\nAge: "; // cin age
int age;
cin >> age ;
cout << "\nFather's Name: "; // cin father name
getline( cin , fatherName );
cout << "\nPermanent Address: "; // cin permanent address
getline( cin , permanentAddress );
When i try to run this snippet along with the whole code.The output program works like :
Enter the full name of student:
Age: 20
Father's Name:
Permanent Address: xyz
If you notice ,the program didn't ask me the full name and went on directly to ask me the age.Then it skips the father's name also and asks the permanent address. What could be the reason for this ?
It is difficult for me to post the whole code because it is too large.
Since you have not posted any code. I am going to take a guess.
A common problem while using getline
with cin
is getline
does not ignore leading whitespace characters.
If getline is used after cin >>
, the getline()
sees this newline character as leading whitespace, and it just stops reading any further.
How to resolve it?
Call cin.ignore()
before calling getline()
Or
make a dummy call getline()
to consume the trailing newline character from the cin >>