getline not working properly ? What could be the reasons? [duplicate]
Asked Answered
G

3

29

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 image description here

output:

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.

Garvy answered 11/7, 2011 at 12:7 Comment(3)
Please just copy and paste program output into a formatted section of your post. Images have the property of disappearing over time, usually yielding red crosses.Cowling
getline not working properly? "select isn't broken"Manse
@Ulterior: Which declarations? int age? getline's, which only takes a std::string as the target? cin's? Really no justification for -1.Cowling
A
99

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

Amelia answered 11/7, 2011 at 12:13 Comment(3)
+1 for psychic debugging.Coetaneous
The problem with ignore is that you don't know how much you need to ignore. It's better to just read the input one line at a time by only ever using getline.Fylfot
or how about: if (getline(cin >> ws, s2)) { getline(cin, s2); }Immerge
F
4

The problem is that you are mixing getline with cin >> input.

When you do cin >> age;, that gets the age from the input stream, but it leaves whitespace on the stream. Specifically, it will leave a newline on the input stream, which then gets read by the next getline call as an empty line.

The solution is to only use getline for getting input, and then parsing the line for the information you need.

Or to fix your code, you could do the following eg. (you'll still have to add error checking code yourself) :

cout << "Enter the full name of student: ";  // cin name
getline( cin , fullName );

cout << "\nAge: ";  // cin age
int age;
{
    std::string line;
    getline(cin, line);
    std::istringstream ss(line);
    ss >> age;
}

cout << "\nFather's Name: ";  // cin father name
getline( cin , fatherName );

cout << "\nPermanent Address: ";  // cin permanent address
getline( cin , permanentAddress );
Fylfot answered 11/7, 2011 at 12:14 Comment(0)
S
3

after the line cin >> age ; there is still the newline character \n (because you pressed enter to input the value) in the input buffer, to fix this you add a line with cin.ignore(); after reading the int.

Shalloon answered 11/7, 2011 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.