getline quirk?

Whenever I use the getline function:
e.g.
1
2
3
		cout << "Enter the name of the shape you wish to call" << endl;
		string FIND;
		getline(cin, FIND);

Almost without fail it will skip the next instance where cin is used.

No teacher has been able to explain to me what causes it. As a result, the only solution I've found is to amend to:
1
2
3
4
5
		cout << "Enter the name of the object you wish to call" << endl;
		string FIND;
		getline(cin, FIND);
                cin.clear();
                cin.ignore();

...which seems a bit messy, but works.

Is there better solution, or is it just something I should learn to add whenever I use getline?
you dont need cin.clear() as far as I know, but you need the cin.ignore().
Post more code. Using cin>> before getline may cause problems, but not the other way around..
Alright - I'll take an excerpt from a different code where I had the problem:
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
	const string degreeCode("PHYS"); 
	// Gather list of courses and their codes from user, 
	// storing data as a vector of strings 
bool not_finished(true); 
	do { 
		cout<<"Please enter a course name (or x to finish): "<< endl;

		//For some reason; the next 3 lines were skipped after the first iteration. This was fixed by inserting the cin.ignore()
		getline(cin,CourseName);	// Get line allows for spaces in the string entry.
		cin.clear();				// "Good practice" - but didn't fix the issue.
		if(CourseName==fin){not_finished=false;}	// Check if the entry is not "finished"
		
                else{					
			cout<<"Please enter the numerical course code: "<< endl;
			cin>> CourseCode;
			cin.ignore();			// Fixed the issue - but unsure why

			ostringstream FCC;		// Create new stringstream Full Course Code (FCC)
			FCC << CourseCode << "\t" << CourseName;		// Assign String Stream FCC to combine degreecode, CourseCode and CourseName
			
			string FullDetail(degreeCode+FCC.str());		// Define the Full Details
			CourseDetails.push_back(FullDetail);		// Add Course Details
			FCC.str("");							// Clear string stream
	
			}				
	}
	while(not_finished); 


So cin.clear() isn't needed, and cin.ignore() is the important one.
Does ignore() clear the input line? I'm still unsure what causes it to skip if it isn't present...
Oh. So as I said, >> is causing the problem, not getline.
clear serves no purpose on line 10.
What ignore() does is remove one character from the input stream. With correct arguments it could remove everything there is (if there is anything at all).

This problem exists because when you enter 123 and hit enter, input stream contains the string "123" and a newline char. cin>> reads the "123" but leaves the newline as it is not a part of the number. Then getline finds a newline and reads it (resulting in an empty line). To prevent this you have to remove the newline before getline is called (which is what ignore does).
Okay - think I understand it. I think in that code above I should have placed cin.ignore() immediately after the getline statement, but it works either way I guess.

Thanks very much.
No, you should place cin.ignore() immediately after the cin >> CourseCode; statement.

The input comes as a sequence of characters, like
3 4 \n J a n e \n

The following code will fail to read it:
1
2
3
4
cout << "How old are you? ";
cin >> age;
cout << "What is your name? ";
getline( cin, name );

That is because the >> is for reading formatted input, and it doesn't read anything more than what it was asked to read.

What this means is that using cin >> age reads "34" from the input, and leaves "\nJane\n". Calling getline() on that returns you the string "" and leaves "Jane\n" in the input.

Now, you may want to argue that the user only typed "34" in response to the first question. That isn't quite true. The user typed "34" and pressed the ENTER key. This is very important to remember:

    The user will always press ENTER at the end of every input.

A good rule of thumb is not to mix formatted and unformatted input methods (>> and getline()). Stick to one or the other. But if you must mix, make sure to do it the Right Way.

1
2
cin >> CourseCode;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

Here is a good thread to read through to understand better.
http://www.cplusplus.com/forum/beginner/18258/#msg92955
Make sure to follow the links given there also.

Good luck!
Thanks - it makes a lot more sense now :)
Topic archived. No new replies allowed.