jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

cin >> string variable will read in leftover whitespace/newline instead of ignoring it #354

Open lslavice opened 2 months ago

lslavice commented 2 months ago

this was what James and Laura figured out using this example:

https://engr101staff.github.io/engr101.org/lobster-exercises/guess_function.html

when the play variable was a string variable. If you run the program (without stepping) and try to enter "y" to play again, the leftover whitespace was already read in, causing play to be an empty string (and hence ending the program before you actually get to enter "y" and have it be read in).

If you step through the simulation and enter "y" before cin is called to read in anything to play, then the whitespace is properly ignored and the program works as intended.

James says there's an inconsistency in where skipws() is used in overloading the >> operator for string variables. (At least, that's what I think he said. :) )