getline() Can Bite My Ass!!!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

getline() Can Bite My Ass!!!

Post by Maevik »

So, I'm working some of the practice programs in my C++ book. These ones have to do with sort / search algorithms; I finished the ones on arrays, and now I'm doing vectors.

I was working on my search function when I came across this problem. It's driving me mad...

Code: Select all

	while( !quit )
	{
		cout << "Enter a string to be searched for: ";
		getline( cin , str );

		searchString( strList , str );

		if( !again() )
		{
			quit = true;
		}
	}  
The first time through, this code works fine, but if I choose to search again, it skips past prompting me to enter a new search string.

if I add a line after getline()
cin.get( discard );

Where discard is a char variable, I can get rid of the '\n' that getline doesn't pick up. It works, but it makes the program pause after the getline() until there's a keypress. I also don't want this. Similarly, I can use cin.ignore( 1000 , '\n' ) but this will also make the program stop and wait for a return press.

I'm at a loss of wtf I should do to instruct the program to take the string, then continue without undesired behavior.
My love is like a Haddoken, it's downright fierce!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: getline() Can Bite My Ass!!!

Post by Ginto8 »

replace cin.get(discard) or cin.ignore() or whatever you're using with cin.sync(). sync() basically just clears the buffer, so there shouldn't be any problems with it. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: getline() Can Bite My Ass!!!

Post by Maevik »

Thanks, but that just made the program behave the same as when I only used getline()

It seems as though, if getline() is in a loop, it will only pause the program for user input on the first run through the loop.
My love is like a Haddoken, it's downright fierce!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: getline() Can Bite My Ass!!!

Post by MarauderIIC »

It shouldn't. Although IIRC some version of MSVS's library have problems with string::getline() [the getline you're using]. What version of MSVS are you using?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: getline() Can Bite My Ass!!!

Post by Maevik »

Visual Studion 2008 (Professional)
9.0.30729.1 SP
My love is like a Haddoken, it's downright fierce!
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: getline() Can Bite My Ass!!!

Post by avansc »

whats wrong with gets()?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: getline() Can Bite My Ass!!!

Post by Joeyotrevor »

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
Post Reply