While loop

Mar 28, 2015 at 10:05pm
Not sure what I am doing wrong, but 1-99 work with this code but not 0 or 100. However if I change to while (a>=0||a<=100) nothing works. I need to have 0 and 100 be inclusive.

1
2
3
4
5
6
7
8
9
10
11
12
  int main()
{
    int a;
    
    while (a <= 0 || a >= 100)
    {
        cout<<"Enter your test score."<<endl;
        cin>>a;
        }   
        
return 0;
}

Thanks for any help you can provide
Mar 28, 2015 at 10:09pm
hmm... no 1-99 does not work with the code you've provided.

1. You're using the wrong loop. You want a do-while loop.
2. As the code is now. you're telling it to run the loop as long as "a" is less or equal to 0 or a is 100 or more. Meaning literally any number other than 1-99.

You want to say. While a is bigger than or equal to 0 AND less than or equal to 100.

1
2
3
4
5
6
7
8
 
int a;

do
{
       cout << "Enter your test score." << endl;
       cin >> a;
} while (a >= 0 && a <= 100);


Hope this helps :)
Last edited on Mar 28, 2015 at 10:11pm
Mar 28, 2015 at 10:15pm
I think it should be a>=0&&a<=100, because you want both to be true. But if I do that, the program doesn't open. But I get no errors.

Is the way the program is written mean it will end if the condition is not met but will keep prompting if the condition is met?
Mar 28, 2015 at 10:17pm
Yes. It will keep prompting if the condition is met, as soon as you input a number that does not meet the condition a>=0&&a<=100 (which is btw the condition you want) then it will exit the loop.

If you're still using a while-loop that might be the problem, because you're not initializing int a;

So if you're using a while-loop still. do int a = 0; But I would advice a do-while loop like I showed you.

If non of it is working and you are still having the problem, please post the entire code.
Mar 28, 2015 at 10:34pm
Thanks the program requires both a while loop and a do while loop doing the same thing. For some reason I thought the condition would end the statement not the other way around. I wrote it at while (a<0||a>100)

Thanks again!!!
Topic archived. No new replies allowed.