-
newbie question
Here's my body:
Code:
{
int i;
int loopcount;
loopcount = 5;
{
while (loopcount-- > 0);
cout << "Please insert a number.\n";
cin >> i;
{
if (i % 2 == 0)
cout << i << " is even.\n";
else
cout << i << " is odd.\n";
}
}
cout << "Alright that's 5, come back later for more.\n";
system("PAUSE");
return 0;
}
Here's what it produces:
Please insert a number.
2
2 is even.
Alright that's 5, come back later for more.
Press any key to continue . . .
What I want it to do:
I 'm trying to making it loop 5 times before quitting but it seems to be ignoring my while command. What am I missing here?
-
I think it's because your decrementing it in the wrong place.
Try this:
Code:
{
int i;
int loopcount;
loopcount = 5;
{
while (loopcount > 0, loopcount--);
cout << "Please insert a number.\n";
cin >> i;
{
if (i % 2 == 0)
cout << i << " is even.\n";
else
cout << i << " is odd.\n";
}
}
cout << "Alright that's 5, come back later for more.\n";
cin.get();
return 0;
}
I'm not sure this will work, i'm still a newby myself, and also try to use cin.get(); instead of system("PAUSE");
-
This:
Code:
while (loopcount-- > 0);
Is equivalent to:
Code:
while(loopcount-- > 0)
{
;
}
That's obviously silly what for you want. Don't put a semicolon after a while loop condition unless you know what you're doing and why you need to do it. Put braces around the statements you want to be a part of the loop.
Code:
while (loopcount > 0, loopcount--);
The above makes even less sense, but I think you're trying to form a for loop. Either way, his loop should work.
What I would rather do is use a for loop like this:
Code:
int i;
...
for(i=0;i<5;i++)
{
// loop body here....
}
-
Code:
while (loopcount > 0, loopcount--);
What's after the closing bracket and before the semicolon is all the loop body (nothing). All that follows is outside the loop.
What you meant to be in the loop body, needs to go inside curly braces.
-
Code:
while (loopcount > 0, loopcount--)
this compares loopcount to 0, then throws that result away, and then stores loopcount in a temp variable, then decrements loopcount, then evaluates the temp variable in the while loop. So while it will iterate five times, it's probably not what you wanted.
-
Code:
{
while (loopcount-- > 0);
The error is simply that you got these the wrong way round. It's supposed to be
Code:
while(condition) {
// body
}
Note: no semicolon after the while.
-
Thank you guys very much, you all are true friends in the world of programming. Here's my final working result.
Code:
{
int i;
int loopcount;
loopcount = 5;
{
while (loopcount > 0,loopcount--)
{
cout << "Please insert a number.\n";
cin >> i;
{
if (i % 2 == 0)
cout << i << " is even.\n";
else
cout << i << " is odd.\n";
}
}
}
cout << "Alright that's 5, come back later for more.\n";
system("PAUSE");
return 0;
}
-
>> Here's my final working result.
Now that it's working, try to go back and understand some of the advice given in the thread on how to make it better.
-
Yes, and for situations such as this in the future you will find that for() loops are much easier to implement and are also much more efficient and useful.
Good luck!
-
The operator , returns the value of the last statement so
loopcount > 0,loopcount--
returns the result of
loopcount--
It n=means - you can safely delete all you have before , and the effect will be the same
For the loop with known number of iterations for loop suits better.