Random Number Gen as function

2019-10-09 01:29发布

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.

2条回答
何必那么认真
2楼-- · 2019-10-09 01:56

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.

查看更多
贼婆χ
3楼-- · 2019-10-09 01:57

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)
查看更多
登录 后发表回答