[Solved] Random Number Gen as function


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.

4

solved Random Number Gen as function