while loop epic fail

Aug 9, 2014 at 2:39am
im new :) to this forum.
so i got a little problem that is a big problem to me cause im detailed oriented.
problem is in the while loop false and true statement.
while (var != 0 || var != 5) it keeps thinking its true even with a local variable "var" before the loop makes it false. so it will not exit or skip it.
but its only when its the Not Equals != logic works fine with == I've been stuck for 3H now and made this account thought it might help some one else as well. any one know why im stuck?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
    {
    int input;
    int var;    
    int number;
    var = 0;
    
while ((var != 0) || (var != 5))
{
    cout<<"type numeric number\n";
    cin >> input;
    cout<<"type numeric number\n";
    cin >> number;
    //if statement to check if its greater then or less then
    if(input>number)
                    {
                    cout<< input <<" is greater then " << number <<"\n";
                    var = 5;
                    }
    else
                    {
                    cout<< input << " is less then " << number <<"\n";
                    var = 0;
                    }
}                

    
system("PAUSE");
return 0;
}
Aug 9, 2014 at 3:03am
Try testing out what exactly it is you are saying with your if statement:

if(var != 0 || var != 5)

if (var is not 0) or (var is not 5)

What is the range of values that can be accepted here?
Aug 9, 2014 at 9:08am
@firedraco
What is the range of values that can be accepted here?
any number other then 0 or 5 will be true. its been a while since i coded so im doing the basics.
but im not understanding why it won't exit when clearly 0 is equal to 0 making logic false and exit.
this should skip the while loop.
1
2
3
4
5
6
// redundant code above......
var=0; // or var =5;

while(var != 0 || var != 5)
{cout<<"bla bla bla hear";}


it doesn't skip the while loop or while loop doesn't see it as false. an i doing something wrong?
Aug 9, 2014 at 9:23am
OR is true, if either is true.
Aug 9, 2014 at 9:37am
@keskiverto
yes.

i might not be explaining my objective very clearly.
hmmmm
does my first program post work for you?
cause i might just have a compile problem/ bug.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
    {
    int input;
    int var;    
    int number;
    var = 0;
    
while ((var != 0) || (var != 5))
{
    cout<<"type numeric number\n";
    cin >> input;
    cout<<"type numeric number\n";
    cin >> number;
    //if statement to check if its greater then or less then
    if(input>number)
                    {
                    cout<< input <<" is greater then " << number <<"\n";
                    var = 5;
                    }
    else
                    {
                    cout<< input << " is less then " << number <<"\n";
                    var = 0;
                    }
}                

    
system("PAUSE");
return 0;
}
Aug 9, 2014 at 11:18am
Hi there,
Your code enters the loop if any of the conditions is true due the logical or:
1
2
var != 0 /* False as var is 0 */
var != 5 /* True as var is 0 and you go in the loop. */

What you're looking for is to enter the loop if all conditions are true,
which means you need a logical and ( && )

1
2
3
...
while (var != 0 && var != 5)
...


Hope this makes it clear :)

Last edited on Aug 9, 2014 at 11:19am
Aug 9, 2014 at 12:43pm
so youre code is saying that if you do not enter 0 and 5 the program will be return to 0?
Aug 9, 2014 at 3:03pm
If var==0, then var!=5 and the condition is true.
If var==5, then var!=0 and the condition is true.
If var is anything else, then the condition is true.

In other words: no matter what value the var has, the condition is always true.

That is not the only problem in your program.
If input is not greater than number, you say that input is less than number. That is not always true. If input is equal to number, it is not greater than number, but not less than number either.

It looks like that you want to quit the loop on inequality. You could do:
1
2
3
4
5
6
7
8
while (true) {
  cin >> input;
  cin >> number;
  if ( number < input ) {
    var = 5;
    break;
  }
}
Aug 9, 2014 at 11:01pm
wow brain fart. thats what happens when you work late :)
i should of did the logic on paper.
thanks
@axtroz
i was looking for all conditions are true other then 0 or 5 (0 or 5 will exit the loop)
@lorence
no the return 0 it was testing why it didn't exit. turns out that 0 != 0 = false but it remained true with 0 != 5, hope i helped some people.
@keskiverto
very true. i actually erased a lot of the code inside trying to find out why it din't work. then i posted.

if i can give cudos i would. guys thanks for the help. back to the books.
Topic archived. No new replies allowed.