-4

The goal of this program is to have a random number generator that I can use in the main through a function. Then the values from the random number generator will trigger an action. This has if statements checking the values from the generator. The problem I am having is both strings, "Numbers 1 - 5" and "Numbers 6 - 10" appear instead of the if statements triggering only what is expected from them which would make only one of the strings appear.

#include <iostream>
#include <string>
#include <chrono>

void main()
{
    void RanNum1to10();
    int RanNum;
    int x;
    std::cin >> x;
    if (x == 2)
    {
        std::cout << "X = 2" << std::endl << std::endl;
        RanNum1to10();
        if (RanNum = 1||2||3||4||5)
        {
            std::cout << "Number 1 - 5" << std::endl;
        }
        if (RanNum = 6||7||8||9||10)
        {
            std::cout << "Number 6 - 10" << std::endl;
        }
    }
system("pause");
}

void RanNum1to10()
{
    (time(0));
    int RanNum;
    int x = 1+(rand() % 9 + 1);
    switch(x)
    {
        case 1:
            RanNum = 1;
        case 2:
            RanNum = 2;
        case 3:
            RanNum = 3;
        case 4:
            RanNum = 4;
        case 5:
            RanNum = 5;
        case 6:
            RanNum = 6;
        case 7:
            RanNum = 7;
        case 8:
            RanNum = 8;
        case 9:
            RanNum = 9;
        default:
            RanNum = 10;
    }
}

Thank you.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Laina
  • 29
  • 1
  • 7
  • 1
    You seriously need to get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the basics again. – T.C. Sep 06 '14 at 03:56
  • Don't edit the question to make the code correct after it has been answered. That'll confuse later readers – phuclv Sep 06 '14 at 05:06

2 Answers2

2

You need to use

    if ( (RanNum == 1) ||
         (RanNum == 2) ||
         (RanNum == 3) ||
         (RanNum == 4) ||
         (RanNum == 5) )

instead of

    if (RanNum = 1||2||3||4||5)

Similarly for the other if statement.

That's one problem.

The other problem is that you have the line:

int RanNum;

in main but you have not assigned any value to it.

You have assigned values to a local variable of the same name in RanNum1to10 but that won't change the value of RanNum in main.

You can address that problem by changing the return vaule of RanNum1to10 to int, and returning the random number from it, and assigning the return value of RanNum1to10 to RanNum.

Change the line

void RanNum1to10();

to

int RanNum1to10();

Change the line

    RanNum1to10();

to

    RanNum = RanNum1to10();

Change the implementation of RanNum1to10 to:

int RanNum1to10()
{
   return (rand() % 10 + 1);
}

Update, in response to comment by OP

Due to operator precedence, the expression if (x = 2||3||4) is equivalent to if ( x = (2 || 3 || 4) ), which is equivalent to x = 1; if ( x ). As you can see, the conditional of such an if statement always evaluates to true.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @Laina that's just the way expressions work in C and C++. If you look up the [operator precedence](http://en.cppreference.com/w/cpp/language/operator_precedence) you'll see your expression is equivalent to `x = (2||3||4)`, and `2||3||4` evaluates to `1`. The result of `x = 1` is `1` or `true`. – Mark Ransom Sep 06 '14 at 04:31
  • Re your update: `=` has a lower precedence than `||` while `==` has a higher precedence. Your statement is *almost* correct. – Mark Ransom Sep 06 '14 at 04:34
  • regardless of operator precedence, `RanNum = (1||2||3||4||5)` or `(RanNum = 1)||2||3||4||5` is still always true because 1, 2, 3, 4 or 5 are all "true" expressions and their arithmetic or is true too – phuclv Sep 06 '14 at 05:09
0

First problem: if (x = 2) should be if (x == 2). Otherwise you're just testing the result of an assignment, which is equal to the number being assigned. The compiler should have generated a warning for this.

Second problem: to test against multiple values, you can't just or the values together with ||. The result of such an expression will be true or false, equating to 1 or 0. You need to expand out each comparison:

if (RanNum == 1 || RanNum == 2 || RanNum == 3 || RanNum == 4 || RanNum == 5)

Of course in this particular case you can simplify the condition:

if (RanNum >= 1 && RanNum <= 5)
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Thats why! Was wondering why if statements cant be if (RanNum = 1||2). Thank you, it helps a lot. – Laina Sep 06 '14 at 04:31