simple? problem

Started by
7 comments, last by crazy_andy 18 years, 7 months ago
hey, i have a problem with this code: for(int i=0;i<5;i++){ cout<<"1: "; cin.getline(bla1,10); cout<<"2: "; cin.getline(bla2,10); cout<<"3: "; cin>>bla3; } if i run this then within the 1st loop everything will work nicely, the problem with second(third,forth,etc...) loop. it just _skips_ the 1st cin.getline(); function. it does, however, display the "1: "(hooray :\). it's the same with all compilers and platforms. i had this problem so many times, but i somehow managed with the goto function(BLAH!), but i have ENOUGH of this! i want help and i want it now! thank you ;)
Advertisement
why are you using
cin>>bla3;
instead of getline?

that will get only one char, anything else is stored in the buffer untill the next input line, which will use that as its data. skipping the line.

(I think) :p its been awhile since I used cmd line
www.stickskate.com -> check it out, some gnarly stick skating movies
oh, i knew i will forgot smthn.
i use cin.getline(); for strings and i use cin>>; for chars and numbers.
in this case, a number.
Quote:i use cin.getline(); for strings and i use cin>>; for chars and numbers.
in this case, a number.


That's pretty much what you're supposed to do, as far as I remember.
cin >> doesn't get "just one char", but you're hitting on the right problem.

cin reads up until the first whitespace (probably the \n newline you put in when you hit enter).

cin.getline reads up until the first newline \n.

The problem is that when cin >> reaches the newline, it doesn't remove it from the stream's buffer! So, that \n stays in there until the next loop where cin.getline sees it and quits.

The solution is cin.ignore()...I believe. Maybe it's cin.clear()? Look at msdn.com for various cin functions, it's a pretty good resource. What you want is to clear the buffer, so that '\n' won't be in there before you start getline()-ing stuff.
XBox 360 gamertag: templewulf feel free to add me!
Yeah. Reading into the numeric variable will leave whatever's past it on the stream - in your case, it likely just starts with a newline, so the next getline() yields an empty line. The important thing to remember is that the stream state doesn't necessarily sync up with what you see on screen, because of line buffering. :)
put a cin.get(); after your cin>>bla3; this will eat the newline for you
yup, it's cin.ignore(); :)
ty!
Not to be offensive, but doesn't using a get method just to clear the buffer smell a little hacky?

I'm not saying my way is any better; in fact, that's how I did it before I knew about cin.ignore() [smile]
XBox 360 gamertag: templewulf feel free to add me!
Quote:
That's pretty much what you're supposed to do, as far as I remember.
cin >> doesn't get "just one char", but you're hitting on the right problem.


I knew it was sometghing like that :p
www.stickskate.com -> check it out, some gnarly stick skating movies

This topic is closed to new replies.

Advertisement