Why won't cin run?
I think my problem is pretty obvious but i don't get why. The cin never runs and the program pretty much skips it. Why?
1 2 3 4 5
|
int points;
while(points < 0 || points > 100)
{
cin >> points;
}
|
What is the value of points before the loop? (Hint: watch out for undefined behavior)
Edit: There was, at one point, a post before this one saying something like:
I set points but it doesn't work.
1 2 3 4 5
|
int points = 50;
while(points < 0 || points > 100)
{
cin >> points;
}
|
|
It does work.
Line 1 : You create points, and you set its value to 50
Line 2 : if points is less than zero, OR if points is more than a hundred, do what's inside the braces.
Is points less than zero? No.
Is points more than one hundred? No.
So should we do what's inside the braces? Is points less than zero, OR more than a hundred? No it isn't.
Last edited on
this is why do-whiles exist. They do the loop body once no matter what.
eg:
do
{
cin >> points;
}
while (points < 0)
Last edited on
Topic archived. No new replies allowed.