for all you c++ gurus.

BT6LW

Senior Member
Joined
Mar 30, 2005
Messages
525
Reaction score
0
Please have a look at this simple piece of c++ code i've been playing with.

#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout << "enter two numberS";
cin >> num1 >> num2;
while (num1 != 0 && num2 != 0)
{
cout << "try again";
cin >> num1 >> num2;
}
return 0;

}
It makes no sense....

the while loop exits when either variable = 0;

i've been fiddling with this for a while, and its driving me insane.


HELP
 
Place parentheses around the conditions in the while statement:

while ( (num1 != 0) && (num2 !=0) )

That should do the trick.
 
Code:
#include <iostream>
using namespace std;

int main()
{
  int num1, num2;
  cout << "enter two numberS";
  cin >> num1 >> num2;
  while (num1 != 0 && num2 != 0)
  {
    cout << "try again";
    cin >> num1 >> num2;
  }
  return 0;

}

Makes plenty of sense, what you want it to do ?
 
try

while ((num1 != 0) || (num2 != 0))

reason:

if num1 = 0 , (num1 != 0) = false, while (false && (num2 != 0)) will always be false, regardless of what num2 is
 
Last edited:
Please have a look at this simple piece of c++ code i've been playing with.

#include <iostream>
using namespace std;

int main()
{
int num1, num2;
cout << "enter two numberS";
cin >> num1 >> num2;
while (num1 != 0 && num2 != 0)
{
cout << "try again";
cin >> num1 >> num2;
}
return 0;

}
It makes no sense....

the while loop exits when either variable = 0;

i've been fiddling with this for a while, and its driving me insane.


HELP

Your logic problem is the way in which C++ interprets the following code segment [while (num1 != 0 && num2 != 0)]. At a binary level, the following happens:

XXXXXXXX (num1) .
XXXXXXXX (num2)
----------
YYYYYYYY
----------

If either of the two variables are zero the binary addition will result in the code returning a zero and hence pop out of the loop. Correct it as per psivius's statement
 
you're all missing the point,

he want's the loop to exit if num1 AND num2 are both 0, so he's used a && , which common sense says you should, but it's wrong

He needs to use a || because of the double negative and/or logic thing (i.e. using != on the two eval tests)
 
Ok.

Then try to initialise the variables on declaration.

int num1=1, num2=1

Beside being good programming practise(always initlise variables) I dont see how this would make a difference as both variable are assigned in the code before they are evaluated for the while loop ?

I could be missing something? haven't coded c++ in 5 years
 
you're all missing the point,

he want's the loop to exit if num1 AND num2 are both 0, so he's used a && , which common sense says you should, but it's wrong

He needs to use a || because of the double negative and/or logic thing (i.e. using != on the two eval tests)

ASSUME = ASS out of U & ME

:)

I asked what he wanted will have to wait for him to answer :D
 
Deenem is right -

I want it to exit the loop if num1 = 0 and num2 = 0....

I'm getting confused as I thought || means or...


goes searching for reference....
 
Deenem is right -

I want it to exit the loop if num1 = 0 and num2 = 0....

I'm getting confused as I thought || means or...


goes searching for reference....

|| means OR

&& means AND

With AND, if one of the operands is false, the result is false

false && false = false
false && X = false
X && false = false
true && true = true

So if ANY of the numbers is zero you exit the loop, it does not matter what the other number is...
 
ok then AND doesn't mean in c++ what I thought it does -

how do I make it that both conditions need to be false before it exits the loop?
 
i'm still confused but thanks people ---- really appreciate the input

|| works like a charm (don't make sense - but it works)
 
What course you doing and where you doing it. Just wondering if you maybe not in my same class.

I take it you using devcpp aswell?
 
it's confusing but;

((x !=0) || (y != 0)) is the same as (!((x = 0) && (y = 0)))

you could have used

while (!((num1 == 0) && (num2 == 0)))

or in 'english' ; 'while it's not true that num1 equals zero and num2 equals zero', which is the same as saying 'while it's true that num1 is not zero or num2 is not zero'
 
Top
Sign up to the MyBroadband newsletter
X