While Loop Does Not End

Mar 29, 2015 at 7:41pm
My while loop does not seem to end:

while (check=1)
{
cout<<"Please enter the number of people: "<<endl;
cin>>people;
if (people<6000&&people>0);
{
check=0
}
else
{
cout<<"Please enter a number between 0 and 6000"<<endl;
}
}

Even if the conditions are met, the check should be set to 0 and the loop should end but it does not seem to end?

Any help,
cheers
Mar 29, 2015 at 7:55pm
= is assignment
== is comparison
Mar 29, 2015 at 8:05pm
that does not seem to make a difference
Mar 29, 2015 at 8:19pm
It certainly does make a difference.

check = 1 is saying that check will always be assigned to 1, so the loop will never end since the condition is always met.

check == 1 is saying, if check equals 1

Can you post the whole code? It seems like you do not have any operation that changes the check variable
Mar 29, 2015 at 9:35pm
Your code would not compile anyway because of the semicolon after your if statement.
Mar 29, 2015 at 10:10pm
You can have an empty body for a conditional.
Some compilers generate a warning as this may indicate a logic error.
Mar 30, 2015 at 12:55am
The actual error would come from the following else a few lines below it.
Mar 31, 2015 at 3:37am
you had put the semicolon in front of 'if condition' . It is wrong . It is one type of function definition semi colon should not be there. And infront of 'check=0' semi colon should be there . Compiler is waiting for end of the statement.
Last edited on Mar 31, 2015 at 3:40am
Mar 31, 2015 at 7:15am
1
2
3
4
5
6
7
8
9
10
11
12
13
while (check == 1)
{
     cout<<"Please enter the number of people: "<<endl;
     cin>>people;
     if (people<6000&&people>0)
     {
          check=0;
     }
    else
    {
          cout<<"Please enter a number between 0 and 6000"<<endl;
     }
}


(Insert if i had a dollar for everytime i meant to put = instead of == here)

Generally you should use the built in formats for code, because it's easier for people to show you what line of code has a problem, for this situation, you have an error on line 1 check = 1 is the incorrect syntax, as it will always evaluate to true, hense your error never ending, as well I don't understand how you compiled, because check = 0 did not have a semi colon on line 7, and on line 5 you have an out of place semicolon, maybe that was meant to be line 7. Your code should work just fine now though.
Apr 1, 2015 at 8:27am
Thanks so much for your help!
Apr 1, 2015 at 4:41pm
Any time
Topic archived. No new replies allowed.