DEV Community

Discussion on: Interesting C++ bug involving `std::cin` for input.

Collapse
 
_hs_ profile image
HS

The space is parameter separator so I'm not supprised that it triggers input in the next loop iteration. What I am supprised is that buffer is not getting emptied on next iteration causing it to read stuff over and over again. But I'm used to garbage collected languages so I might be expecting more than I should. Anyways try clearing the input buffer on last line of loop, see what happens. I would assume that a single unput input of 100000000000 would overflow uint couple of times and on next interation it would accept new input. Would also expect that empty input (when you hit space after answer) for uint would be 0 or some garbage numeric value not overflowing the buffer again multiple times.

Collapse
 
baenencalin profile image
Calin Baenen

How do you clear it?
The ignore method? (Or is that slightly different than what I want?

Collapse
 
_hs_ profile image
HS • Edited

I think that comment from @pgradot is the way to go. Check for failure, then clear the buffer, then ignore stuff, and finally do the rest - output messages and ask for inputs. Insted of clearing at the end as I suggested it's probably correct to first validate, clear, ignore.

So not just 'ignore' method but combination as described in mentioned comment links.