user3290356
user3290356

Reputation: 95

double conditions don't work in c++

First: I'm begginer. :) I've got a problem with my c++ code.

#include <iostream>
using namespace std;  
int main()
{
    int a,b;
    do {
        cout << "a= ";
        cin >> a;
        if (a<=0) {
            cout << "This number is not positive!\n";
        }
    }
    while (a<=0);
    do {
        cout << "b= ";
        cin >> b;
        if (b<=0) {
            cout << "This number is not positive!\n";
        }
    }
    while ((a==b) and (b<=0));
}

Have you got any ideas?

Thanks!

Upvotes: 0

Views: 89

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110738

It is not possible for that condition to be true. We already know that a is positive, so it can't both equal b and b be negative.

Sounds to me like you want or instead. This would mean that b also has to be positive and must not be the same as a. Note that it is typical to use && instead of and and || instead of or:

while ((a==b) || (b<=0));

Think about it like this: we can't to continue asking for b if they input is negative or if the input is the same as a.

Upvotes: 5

Related Questions