Skipping input?

Pages: 12
Nov 24, 2011 at 9:22pm
it still wants to go to the debug before I have a chance to input
Nov 24, 2011 at 11:51pm
If anyone else can help, itd be greatly appreciated
Nov 25, 2011 at 12:17am
then I guess its skipping the getline line completely at first,
Nope, the computer will do exactly as you tell.
What is happenning is that you've got an '\n' that remains in the buffer, so when getline reads, it reads till the end of line (it reads nothing in this case).
Try in.ignore(); or in.ignore( numeric_limits<streamsize>::max(), '\n' );
Nov 25, 2011 at 12:20am
would I put that before the getline line? I'll try that then
Nov 25, 2011 at 12:21am
Don't srand() anywhere but somewhere at the top of main().

Lines 28 and 36 do the same things. Change 36 to } while (true);. (You don't technically need line 32 either.)

Line 75 tests something that you already know as a consequence of line 44. All you need is an else.

The problem you are having is that somewhere else in your program you are not clearing the newline from the user's input before you call this function again.

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

As a consequence, you must always get rid of the newline in the input after the user gives you input. http://www.cplusplus.com/forum/beginner/20577/#msg107828

Hope this helps.
Nov 25, 2011 at 12:22am
It works for the first time it goes through the loop, but after that, no matter what I put (even if its legal) It will say Nope. Try Again.
Nov 25, 2011 at 12:23am
Actually you should put that right after you do the in>>var;
(or use something like
1
2
while( getline(in, line) and line=="" )
 ; // <-- do nothing 


Before line 25, it should work
Nov 25, 2011 at 12:24am
But if the user presses enter only once, then why would it be residual? and why doesnt it mess up at any other part of the code?
Nov 25, 2011 at 12:25am
You aren't paying attention. The problem isn't in your function. It is that somewhere else in your program you are leaving a newline in the input after getting something from the user. Find that and fix it.
Nov 25, 2011 at 12:29am
How can I identify where a newline is being left in the input?
Nov 25, 2011 at 12:30am
And if that were true, wouldnt the same thing happen with cin >>?
Nov 25, 2011 at 12:36am
Nope, because the operator>> ignores whitespaces http://cplusplus.com/reference/iostream/manipulators/skipws/
Nov 25, 2011 at 12:45am
"if that were true"?
I'm not the know-nothing troglobite asking for help, but ignoring all the information feed him.
Bye.
Nov 25, 2011 at 12:46am
I tried using the cin.ignore( numeric_limits <streamsize> ::max(), '\n' ) line before every input, and it solved the line being displayed where I didnt want it, but now I have to type in the command 2 times everytime I want to do something
Last edited on Nov 25, 2011 at 12:51am
Nov 25, 2011 at 12:46am
@Duoas: Yeah, sorry, that came out wrong DX
Nov 25, 2011 at 12:57am
hmm... Actually, I put it only where the residual newline seemed to be actually affecting it, and it seems to work fine now
Nov 25, 2011 at 2:49am
Sorry to have been grouchy. I'm glad you got it figured out.
Nov 25, 2011 at 3:06am
Ehh, dont worry about it, I guess I probably should have paid more attention to the whole residual newline thing :P I just didnt really understand it though xD
Topic archived. No new replies allowed.
Pages: 12