When I run the code, what's supposed to happen is that when I input three numbers separated by spaces (e.g. "23 4 5 <enter>"), it outputs "no more input\n\nReady\n", when I input four or more numbers (e.g. "23 4 5 34 65 <enter>"), it outputs "is more input\n\nReady\n":

#include "stdafx.h"
#include <iostream>
#include <limits>
using namespace std;
void main(){
	int a[3];char b[2];
start:
	cout<<"\n\nReady\n";
	cin>>a[0]>>a[1]>>a[2];
	cin.get(b,2);
	if(*b==NULL) cout<<"no more input";
	else cout<<"is more input";
	cin.ignore(numeric_limits<streamsize>::max(),'\n');
	goto start;
}

But it doesn't work if I input three numbers: it outputs "ready\n\nno more input\nready\n\nno more input\n..." without waiting for me to press enter.

Thanks in advance for answering my question.

You did not flush the std input buffer..Just think..

Thanks for answering, but I really don't understand: to get to the "goto", the program has to go through "cin.ignore(numeric_limits<streamsize>::max(),'\n');", which removes everything up to the '\n' added to the end of the stream when the user pressed enter. After they pressed enter, they wouldn't be able to (at least I didn't) enter anything else.

So why isn't the stream empty?

Reread the get member function specification more carefully.
If cin.get(b,2) can't obtain the next char from the cin, it sets failbit for cin. When the cin stream is in failed state, no more operations performed until you clear the cin state (by cin.clear(), for example). Now cin.ignore and cin>>... ignored - that's why the program performs loop forever.

Counter-question: what for start label and goto?

for (;;) { // loop forever: it's a clear and honest construct
...
}
// or (I don't like this)
while (true) { // longer and precious
...
}

Counter-question: what for start label and goto?

for (;;) { // loop forever: it's a clear and honest construct
...
}
// or (I don't like this)
while (true) { // longer and precious
...
}

I agree that you should not be using goto . And I have no idea why a while is precious, and longer.
I prefer the while() to the for() -- it's more logical to me.
The argument for the while() is a TRUE or FALSE expression, which true fulfills exactly.
But for() is generally meant to loop a specific number of times, and leaving out all 3 expressions seems to be a kludge to get an endless loop.

I agree with WaltP - the for( ; ; ) is really less clear your intent than while(true) - if you think that's too long, then while( 1 ).

Really, how often do you really mean to have a loop be infinite? When you see either of those constructs, that means you gotta dig through the code to find where the writer is busting out with a break statement. The beginning of kludgy code. The loop statement should tell you why the loop ends.

I can't think of many pieces of code that really should be infinite loops. Heart pacemaker control - definitely. I want that thing still ticking long after I'm planted in the dirt. :P

for (;;) { /* Of course, tastes differ. Some remarks.

The for loop is not (only) "loop a specific number of times" for me (quite the contrary - remember containers traversal pattern). It's the most generic loop construct in C and C++. So for(;;) construct gives (me) a brief and clear (and stylistic neutral) demonstration of "loop forever" programmer's intension.

On the other hand I don't like constructs if (constant-expression) or while (constant-expression) . As usually, they are erroneous or looked as truism. The while (condition) construct means (for me) literally "loop while computed condition is true" - but it's a very strange phrase "loop while constant true is true"... */} ;)

Yet another remark.

...
Really, how often do you really mean to have a loop be infinite? When you see either of those constructs, that means you gotta dig through the code to find where the writer is busting out with a break statement. The beginning of kludgy code. The loop statement should tell you why the loop ends.
...

Loop forever pattern is a natural construct in operational/embedded systems and servers/services programming. Besides that there is "Loop and Half" pattern:

loop
    prepare (read, compute etc)
    if (condition) break;
    process
end loop

So the last sentence above with "should" looks at least opened to questions. ;)
Apropos, let's remember: the C and C++ for loop is not a counter-controlled loop at all. Its syntax/semantics pattern is:

for ([prelude]; [condition | true]; [interlude])
    body

for ([prelude]; [condition | true]; [interlude])
body

That almost sounds dirty!;)

Thanks! That's solved everything. There's just too much to remember when using iostream, but I'll learn all the potholes... one day.

Counter-question: what for start label and goto?

I know, I know, "it's good practice and you should get into the habit," but that's just the way my brain works. I think do this, then this, then go back to there and start again. And since its just a few lines of code, I knew I wouldn't get confused.

But anyway, thanks!

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.