I'm having trouble finding information on the implementation of "or". I want to use it in the do while loop below.

do {
    cout << "Please enter 1 for random or 0 for pseudorandom." << endl;   //bool, 1 is TRUE, 0 False
    cin >> random;
    } while (random !0||!1);

Recommended Answers

All 4 Replies

You would have to have your while() as

while(random != 0 || random != 1);

but this wouldnt work as you want it to. You need to use and (&&)

while(random != 0 && random != 1);

Like sfuo said, the || and && operators are for boolean values, so, you need to put two conditions (or expressions that evaluate to a bool value) on the two sides of those operators. In this case while(random != 0 && random != 1) , which reads as "while 'random' is not 0 and 'random' is not 1".

It is also good to know that the || and && operators are also short-cut operators. It means that, for example, if the first condition already evaluates to true in a or-statement, the second condition will not be evaluated at all because it is not needed to get the result of the overall expression. Similary, in a and-statement, if the first condition evaluates to false, then the second condition is not evaluated at all. This can be very useful, for example, to check if a pointer is NULL before dereferencing it:

while (ptr != NULL && *ptr != some_value )

The second condition *ptr != some_value will not be evaluated if the first one is false (meaning that the pointer is NULL, in which case you couldn't dereference it).

commented: Good to know about the pointers +8

sfuo is may be right.The will be....

do {
    cout << "Please enter 1 for random or 0 for pseudorandom." << endl;   //bool, 1 is TRUE, 0 False
    cin >> random;
    } while (random !0||random !1);

If you look at my post I gave you the answer.

The statement random !0 should give you an error. What you need is random != 0. Same thing goes for random !1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.