13

I tried to test this myself before asking on the forum but my simple code to test this didn't seem to work.

#include <iostream>
using namespace std;

int main() {
cout << "Enter int: ";
int number;
cin >> number;
if (number==1||2||3) {
    cout << "Your number was 1, 2, or 3." << endl;
}
else if (number==4||5||6) {
    cout << "Your number was 4, 5, or 6." << endl;
}
else {
    cout << "Your number was above 6." << endl;
}
return 0;
}

It always returns the first condition. My question is, is it even possible to have more than 2 OR conditions? Or is my syntax incorrect?

jww
  • 97,681
  • 90
  • 411
  • 885
  • 9
    Your syntax is incorrect. `if(number == 1 || number == 2 || number == 3)` – Retired Ninja Jan 08 '12 at 21:23
  • I would like to see an example with even two that works, this will NEVER work. What are your expectiations on evaluation order, and what would you thing `if(2)` would do? – Martin Kristiansen Jan 08 '12 at 21:25
  • 3
    @MartinKristiansen Using deduction skills, you might be able to guess that I want an input of 1, 2, or 3 to return the first condition; an input of 4, 5, or 6 to return the second; and an input of anything else to return the third condition. Instead of bashing someone who is clearly new to C++, you could suggest a better way to write my code. –  Jan 08 '12 at 21:30

9 Answers9

15

You need to code your tests differently:

if (number==1 || number==2 || number==3) {
    cout << "Your number was 1, 2, or 3." << endl;
}
else if (number==4 || number==5 || number==6) {
    cout << "Your number was 4, 5, or 6." << endl;
}
else {
    cout << "Your number was above 6." << endl;
}

The way you were doing it, the first condition was being interpreted as if it were written like this

if ( (number == 1) || 2 || 3 ) {

The logical or operator (||) is defined to evaluate to a true value if the left side is true or if the left side is false and the right side is true. Since 2 is a true value (as is 3), the expression evaluates to true regardless of the value of number.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 2
    Don't you put spaces around `||`? It's a little bit hard to read, don't you find? :-) – Sergio Tulentsev Jan 08 '12 at 21:25
  • No I don't use spaces around any of my operators (+, -, ||, <, etc). I find it more hard to read with all those spaces. –  Jan 08 '12 at 21:26
  • 1
    Or better: `number >= 1 && number <= 3`. Put in the spaces btw. – Fred Foo Jan 08 '12 at 21:26
  • 1
    No matter how you format code there's always going to be someone who would prefer it a different way. I generally prefer spaces around things like `==` and `||` but don't put a space between `if` and `(`. I also don't put the `{` at the end of the line, but it doesn't bug me nearly as much as it used to. :) – Retired Ninja Jan 08 '12 at 21:34
3

While you can (as others have shown) re-write your tests to allow what you want, I think it's also worth considering a couple of alternatives. One would be a switch statement:

switch (number) { 
    case 1:
    case 2:
    case 3:
        cout << "Your number was 1, 2, or 3." << endl;
        break;
    case 4:
    case 5:
    case 6: 
        cout << "Your number was 4, 5, or 6." << endl;
        break;
    default:
        cout << "Your number was above 6." << endl;
}

Personally, I'd probably do something like this though:

char const *msgs[] = {
    "Your number was 1, 2, or 3.\n",
    "Your number was 4, 5, or 6.\n"
};

if (number < 1 || number > 6)
    std::cout << "Your number was outside the range 1..6.\n";
else
    std::cout << msgs[(number-1)/3];

Note that as it stands right now, your code says that 0 and all negative numbers are greater than 6. I've left this alone in the first example, but fixed it in the second.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Right. For my application though it doesn't matter what the numbers evaluate to be, because they are always within the range 1-9, so simply using the first example works for me. Also I would feel like I was stealing if I were to use the second example since I didn't write it myself. :P –  Jan 08 '12 at 21:45
1

Try separating all of them out. I am pretty sure your syntax is incorrect

#include <iostream>
using namespace std;

int main() {
cout << "Enter int: ";
int number;
cin >> number;
if ((number==1)||(number==2)||(number==3)) {
    cout << "Your number was 1, 2, or 3." << endl;
}
else if ((number==4)||(number==5)||(number==6)) {
    cout << "Your number was 4, 5, or 6." << endl;
}
else {
    cout << "Your number was above 6." << endl;
}
return 0;
}
schwert
  • 61
  • 6
1
if (number==1||2||3)

This code can be parenthesized like

if ((number==1) || (2) || (3))

or in other words if(number == 1 || true || true), always resulting in true. Compare one by one (number == 1 || number == 2 || number == 3) or with ranges (number >= 1 && number <= 3).

AndiDog
  • 68,631
  • 21
  • 159
  • 205
0

For a long list of options, you might wish to put all of your options in a static array or vector and check if they contain an option:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void menu_prompt(vector<int> list) {
    static vector<char> menuOptions{'P', 'A', 'M', 'S', 'L', 'Q', 'p', 'a', 'm', 's', 'l', 'q'};

    char user_input{};
    do {
        cout << "P - Print numbers" << endl;
        cout << "A - Add a number" << endl;
        cout << "M - Display mean of the numbers" << endl;
        cout << "S - Display the smallest number" << endl;
        cout << "L - Display the largest number" << endl;
        cout << "Q - Quit" << endl << endl;
        cout << "Enter your choice: " << endl;

        cin >> user_input;
    } while (std::find(menuOptions.begin(), menuOptions.end(), user_input) == menuOptions.end());
    if (user_input == 'P' || user_input == 'p') {
        //user_choice_print_numbers(list);
    }
    else if (user_input == 'A' || user_input == 'a') {
        //user_choice_add_numbers(list);
    }
    else if (user_input == 'M' || user_input == 'm') {
        //user_choice_mean_numbers(list);
    }
    else if (user_input == 'S' || user_input == 's') {
        //user_choice_smallest_numbers(list);
    }
    else if (user_input == 'L' || user_input == 'l') {
    //  user_choice_largest_number(list);
    }
    else if (user_input == 'Q' || user_input == 'q') {
        //user_choice_quit();
    }
}
Alex
  • 877
  • 5
  • 10
0

You have to compare like this

if (number == 1 || number ==2 || number == 3){...} You have to put number == every time you compare it.


A Pro Tip Always initialise variables, int number{0}; Don't declare var without initialising them.

ripped guy
  • 115
  • 7
0

It looks like you'd like to chain the ors in this:

if (number==1||2||3)

The above should be written as

if (number==1 || number==2 || number==3)

or, by checking if number is in the range [1,3]:

if (number>=1 && number<=3)

If you'd like to chain many ors (that are not ranges), you could create a helper template function using a fold expression (C++17):

Example:

#include <functional> // std::equal_to
#include <iostream>

// matching exactly two operands
template<class T, class BinaryPredicate>
inline constexpr bool chained_or(const T& v1, BinaryPredicate p, const T& v2)
{
    return p(v1, v2);
}

// matching three or more operands
template<class T, class BinaryPredicate, class... Ts>
inline constexpr bool chained_or(const T& v1, BinaryPredicate p, const T& v2,
                                 const Ts&... vs)
{
    return p(v1, v2) || (chained_or(v1, p, vs) || ...); // fold expression
}

int main() {
    // check if 6  is equal to any of the listed numbers
    if constexpr (chained_or(6, std::equal_to<int>(), 1,3,4,6,9)) {
        std::cout << "6 - true\n";
    }

    // check if 7 is any of the listed numbers
    if constexpr(chained_or(7, std::equal_to<int>(), 1,3,4,6,9)) {
        std::cout << "7 - true\n";
    }
}

When chained_or is instantiated, the fold expressions will unfold to:

(6 == 1 || 6 == 3 || 6 == 4 || 6 == 6 || 6 == 9) // true

and

(7 == 1 || 7 == 3 || 7 == 4 || 7 == 6 || 7 == 9) // false
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0
if (number > 0 && number < 4) {
    cout << "Your number was 1, 2, or 3." << endl;
}
else if (number > 3 && number < 7) {
    cout << "Your number was 4, 5, or 6." << endl;
}
else if(number > 0) {
    cout << "Your number was above 6." << endl;
}

Is my syntax incorrect?

Yes, please know that what you experienced happened because (2) and (3) evaluates to true. Instead you would do number == 1 || number == 2 || number == 3

Fabián Heredia Montiel
  • 1,687
  • 1
  • 16
  • 30
0
number == 1 || 2 || 3

is equivalent to

((number == 1) || 2) || 3)

and as the the result of the || operator is 1 if either its left or its right operand is different than 0, the expression above always evaluates to

1

so what you really want is the following expression

number == 1 || number == 2 || number == 3
ouah
  • 142,963
  • 15
  • 272
  • 331