Simple "while" loop - won't work on Mac/Win32 AT ALL!

Hi all. Just wrote a VERY simple app. I have tried this in both VS 2008 *and* Eclipse; it won't work in EITHER (or compiling it from G++ in terminal on my Mac!).

What on EARTH is going on?!!!

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
	int counter = 0;
	while (counter < 1000);
	{
		counter++;
		cout << counter;
	}
	return 0;
}


I have gone through this with a fine tooth comb - there seems to be nothing amiss.
It actually works. I did not compile it but I just looked at it and I know that this compiles fine and actually executes.

Let me translate your code on how the compiler reads it.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
	int counter = 0;
	while (counter < 1000) //Please welcome infinite loop.
        ;
	{
		counter++;
		cout << counter;
	}
	return 0;
}
Hey!. Well I compiled it on: Xcode (Mac), Visual Studio '08 (PC), Eclipse (Mac) and also via g++ (Mac - command line) and it doesn't work on ANY platform whatsoever - it just hangs with no output whatsoever. WEIRD.

PS: It's not an INFINITE loop; the 'while' loop exits, when it reaches 1000.
Last edited on
Why do you think it hangs?

it's because the loop never ends. If the loop exits then it surely will print something.

How about you remove the semi-colon after the while statement?
1
2
3
4
5
while (counter < 1000)
{
	counter++;
	cout << counter;
}
Ahhh! :-P grrr silly semicolon!. I had assumed you had to end all lines with ';' - what a mistake-a-to-make-a! :-). Thanks!.
That is the common misconception.

You do not end all lines with a semi-colon. You end all statements with a semi-colon.
Thankyou so much for your help, Vince. I would have been chasing my tail ALL NIGHT, and it would have probably ended in much frustration. It's often the simplest errors that cause the most problems, because they are SO small and often go unnoticed!.

Thanks.
Topic archived. No new replies allowed.