I'm a total beginner with programming but I'm currently taking a summer course in C++ programming. At the moment I'm trying out some old exams in order to study for my upcoming exam.
However, I have run into a question that I do not understand how to solve and neither do my class mates. I would like to understand this and I therefore turn to you guys for an explaination. :)
I have the following limitations in this task: • You are only allowed one variable (int).
• You are only allowed declaration, assignment and cin, cout statements (no loops!)
The task is to write a small program that gives the below output: Enter integer: 94
You entered: 94
Enter five integers: 1 96 12 9 4
You entered: 1 96 12 9 4
And I got the following hint: Read up on the input buffer...
The code above of course do not work since it only stores the last value in input and not all five integers. I have tried to read up on the input buffer but I cannot at all understand how it should be possible to read five integers only using one variable and no loops. :/
Can someone please explain how to do this and why it works?
Thanks in advanced! :)
The trick is that the input is (by default) line-buffered.
So when the user types "1 96 12 9 4" and presses Enter, then ALL that input is now available to your program.
So, print the prompt, then cin >> input to wait for (and retrieve) the first number that the user just typed.
The rest of the numbers are still there, ready to be cined, but you can now print stuff to the screen and just read the stuff at your leisure. Get it? You don't have to read the rest of the waiting input before you print anything.
So, after getting the first number, print out "You entered: " and the input value, but do not print a newline (or endl).
Read the next integer (since it is still there waiting for you to read it). And print it.
Do it again.
And again.
And again. (This makes five integers you've read and printed total.)
Just to verify that I understood you correctly:
The input stream is always buffering an entire line, so when I'm entering 1 2 3 4 5 it actually buffers the entire line eventhough I only read the first integer (1) to my integer variable?
And then when I read from cin to variable again it takes the second integer and do not wait for user input again?
So if I understood you right the following code should solve this task?
@LendraDwi
That would work, but it would be an extra step to perform when the >> operator does it for you.
@fisken
Yes. You can think of input as a line of people standing at your door. You can open the door and admit as many people as you would like at any time*. You can process the people (turn them into flowers or something) and then send the flowers out the back door at your leisure.
*The only limiting factor is whether or not there are (any or enough) people at the door when you want them. That's when your program waits until someone shows up.
That is, there even though there is a line of people (or numbers) waiting at the door, you don't have to let them all in at once. Take them one at a time (just like the doctor's office).