Problem with stream input

Pages: 12
Mathhead200 wrote:
... including your input, output, and expected output.

Excuse me.

1
2
if( cin.good() )
  ... cin.get( ... );


Bam, that's what I wanted. Okay, I'm going to check that when I can.
closed account (3hM2Nwbp)
I'm fairly sure that istream::get leaves the newline character in the stream rather than extracting it...so your get would be delimited by '\n' on all subsequent 'gets' after the first.

You might consider ignoring characters in the stream up until '\n' after each 'get'.

 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
I already suggested this newline thing but was totally ignored...
Last edited on
Apparently getline() extracts the newline and discards it.
closed account (3hM2Nwbp)
Ah sorry, I didn't see that, despite the large arrow pointing to it in the comment.
Sorry, you didn't explain why you were ignoring the newline so I had no idea what it was doing.
cin.get() takes the characters from the input stream, but leaves the carriage return (newline) in the stream.
the next time it tries to get characters from, the stream, it find the carriage return (newline) as the first character in the stream - which means that input is immediately finished and it returns with no
characters - but leaves the carriage return (newline) in the stream - as you can see you will enter an infinite loop.
That is why you need to get rid(ignore) the newline character to allow the user a chance to enter new input.
Topic archived. No new replies allowed.
Pages: 12