while loop does not end

Jan 21, 2017 at 10:25am
Hi. Why does my while loop never end? Even if I enter 0 , it still lets me enter. The program should end, when I at least type one time 0 (either in "food", or "people").
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
   int main ()
{
    int food, people, days=0 ;
   while ((food!=0) || (people!=0))
   {
       cin>>food>>people;
days++;
   }
cout<<days;
return 0;
}
Last edited on Jan 21, 2017 at 10:33am
Jan 21, 2017 at 10:47am
The loop will stop if you enter 0 for both food and people.

You need to initialize food and people before the loop (or use a do-while loop) to prevent that they are used before being initialized.
Jan 21, 2017 at 10:51am
Thanks. I don't really understand how to initialize ? Can you please show me? Sorry, i am new to programing and don't have any examples. Searched the web, but could not find an easy to understand example.
Last edited on Jan 21, 2017 at 10:55am
Jan 21, 2017 at 10:57am
Initialization just means you give a variable a starting value (like you do with days).

If you don't do this your program might not work properly. When I tested your program it never even entered the loop because of this.
Jan 21, 2017 at 10:57am
Maybe i should use " while ((food>0) || (people>0)) " ?
Jan 21, 2017 at 10:59am
Hello snekus001,

Try initializing your variables before you use them. As your program is written "food" and "people" have unknown values and no guarantee that the while condition will work. When I tried compiling your original code I received errors for uninitialized variables.

You could also try: while ((food!=0) && (people!=0)). I have seen times where "&&" has to be used with two "!=" to work correctly.

Remember both "food" and "people" have to be zero for the while loop to fail. And the way the while is written "days" will likely be one more than you want.

Hope that helps,

Andy
Jan 21, 2017 at 11:01am
First, none of the variables are initialised. When reaching line 9 for the first time, what will happen - will the loop execute or will it end? There's no way to tell, since the contents of the variables are unknown (garbage).

As for the loop condition itself, it uses the logical OR operator || to combine two conditions.

Let's look at an example:
1
2
3
4
5
food    people    (food!=0)    (people!=0)    ((food!=0) || (people!=0))
  5       5         true         true                  true
  5       0         true         false                 true
  0       5         false        true                  true
  0       0         false        false                 false


The only case where the whole condition is false is where both of the conditions are false.

But if the operator || is changed to &&, things are different. The condition as a whole is true only when both the conditions are true. That means the condition as a whole is false when either of the conditions are false.
1
2
3
4
5
food    people    (food!=0)    (people!=0)    ((food!=0) && (people!=0))
  5       5         true         true                  true
  5       0         true         false                 false
  0       5         false        true                  false
  0       0         false        false                 false


The remaining question is the required outcome. Is the line days++; supposed to be executed when either/both the values are zero? Or not?
Jan 21, 2017 at 11:17am
food and people are not initialize so they will have a garbage value , initialize them with 1 or whatever value you want

int food=1,people=1;
Last edited on Jan 21, 2017 at 11:17am
Jan 21, 2017 at 4:17pm
Thank you all :)
Topic archived. No new replies allowed.