Suppose that you have the following C++ Code segment. Show the contents of each variable and the contents of the Input Buffer after each C++ statement executes.
char ch1, ch2, ch3;
int num;
string data;
Data Entered From the Keyboard: D$121TQ:XM (No Spaces)
cin >> num; num = (what is variable) Input Buffer = (what is output)
cin.ignore(3); (no variables) Input Buffer = 21TQ:XM
cin.putback(ch2); (no variables) Input Buffer = $21TQ:XM
getline(cin, data); data = (variable)
Input Buffer = (output)
The ones I have figured out the variable and out of are already answered, the ones I have question marks next to I haven't figured out and need help. Its basically a fill in the blank question.
.peek() returns the next available char but doesn't remove it. .get() returns the next available char and does remove it. Ignore removes the next char.
4) cin of an int is weird. its going to read the whole thing and consume it all. However if it had started with a valid int and had more junk, eg 123xxx:fubar! it would give you 123 as the int and leave the rest of the buffer intact. It will usually give zero for your case (no valid int, everything consumed) but I am not sure if that is dictated or compiler chosen.
7) is important. getline and cin are at odds, look up online about using both together. The long and short of it is that cin requires you to type enter, which puts a new line in the buffer as well. You need to track IB by that new line here. cin would leave the new line, getline consumes it, so IB is actually for all the cin answers when you think it read all the stuff, it will actually be sitting on the new line (which next cin will ignore as whitespace). getline will read the newline, the buffer is *empty*.