getline


Hello, i've used getline several times in my code witout any problems, but for some reason when i step through my code to the line below with in the IDE it doesn't actually pause for any input at the position ??. just don't get it????
The code works okay in every other way?. The variable is declared with

string sString;

and the string.h is included. Is this function temperamental at all??.
Like i said, i have used it in other places in the program without any bother?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

.
.
  switch( sub2)
        {
	    case 1:
		{
		    cout << "\n\n\n" << "Enter name to search for  ";  
	            getline( cin,sString) ; 
		    cout << " Titles found for book  :" <<  sString ;
						   
		    do
		     {.......
The cin>>sub2; statement that you probably have before entering the switch, leaves a '\n' in the input buffer. getline sees that '\n' and assumes you've entered an empty string and pressed enter. Fix it by adding cin.get(); before the call to getline (in order to get rid of that '\n').
Last edited on
You probably have some input before this code (My guess would be cin.get() ). That input leaves a '\n' which is found by getline, and an empty string is read. To solve this add cin.ignore before getline. This will remove chars from input stream.
Thanks guys, that did the trick.
Topic archived. No new replies allowed.