int x,y;
while(cin>>x>>y)
{
if(x>y)cout<<x<<"\n";
else if(y>x)cout<<y<<"\n";
}

This seems clear,but when I entered this:

2     4     6

and hit enter,
I get this:

4

after which I am being asked to enter something again.
I put 8,
and it prints 8.
But what is exactly happening here?
How can it accept more than 2 numbers,hold one somewhere,then later use it when the fourth number is used?
I am muddled up?

It first reads 2, and stores it in x. It then reads 4 and stores it in x. Reading is done so now it executes the body. The 6 you entered also, does not get read into x or y just yet, because the stream already read 2 and 4, so the 6 stays in the stream for later, so when you entered 8, in the stream there will be 6 and 8 now, 6 gets read into x and 8 gets read into y, and the body executes.

Consider std::cin as a viewer over a large input stream where the stream is a sequence of bytes. What you get is a pointer at the current location in the stream and a way to manage about that point. cin is aware of types; this come from template magic and can just be assumed at this point. Given this understanding of types you can ask to extract an int from the stream and cin knows how to go about that for you.

As an example, consider your input as the stream:

stream -> [2 4 6]
           ^ (initial cin position)

Now cin is whitespace delimited by default which means that it will stop processing when it sees a space, tab or other whitespace character. So when you ask for cin to extract x via cin>>x what happens is the stream is read until one of two things is true:
- whitespace is encountered
- a non-integer character is encountered (remember, cin knows about types)
So now, the stream looks like:

stream -> [2 4 6]
             ^ (new cin position)
x <- 2 (x is assigned the value read from the stream)

Same thing happens when you extract y. Which moves the stream to

stream -> [2 4 6]
               ^ (new cin position)
x <- 2 (x is assigned the value read from the stream)
y <- 4 (y is assigned the value read from the stream)

Your processing now takes place and cin begins to process the stream once again. However, only a single number is left there so x gets that value and cin waits for the stream to get more data to read (which happens when you enter the value 8).

One note: Generally console input is line buffered which means the stream is filled one line at a time by the console process. That event is triggered when you type a newline character (the 'enter' key). This is why even though cin is waiting to read the next value it cant extract the 8 until you press enter to fill the stream buffer.

So from what I understood,
when we type anything and hit enter,it gets loaded in the input stream.
cin reads from this stream,into the variable(s)we have given to it,which is governed by spaces.

It also explains why I don't get later prompts(from ,following cin statements) when I have entered too many things followed by spaces.

Hey wait up!!!
So if I enter a non integer like 'h' for an "integer x"(int x),it will get loaded in the input stream and obviously x will not take up that value.

Now will 'h' simply lie around in the stream?
Now that is what we must clear to use another fresh cin?

Yes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.