I'm trying to make a program to generate 2 random numbers and print them to the screen. This is achieved by calling the Numbers function twice and assigning the value to num1 and num2 then calling PrintMsg twice also with those variables but instead the function prints the first value twice.
In the debugger num1 and num2 are being set to 2 different numbers and the mode variable is being successfully passed through to the PrintMsg function.
// C program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include<time.h>
int Numbers() {
bool valid = false;
int randNum;
srand(time(0));
while(valid != true) {
randNum = rand() % 100;
if (randNum > 0 && randNum <= 6) {
valid = true;
}
}
return(randNum);
}
void PrintMsg(int x, int mode) {
if (mode == 1) {
switch(x) {
case 1:
printf(" %d ", x);
break;
case 2:
printf(" %d ", x);
break;
case 3:
printf(" %d ", x);
break;
case 4:
printf(" %d ", x);
break;
case 5:
printf(" %d ", x);
break;
case 6:
printf(" %d ", x);
break;
}
}
else if (mode == 2){
switch(x) {
case 1:
printf(" %d ", x);
break;
case 2:
printf(" %d ", x);
break;
case 3:
printf(" %d ", x);
break;
case 4:
printf(" %d ", x);
break;
case 5:
printf(" %d ", x);
break;
case 6:
printf(" %d ", x);
break;
return;
}
}
int main(void)
{
int num1;
int num2;
num1 = Numbers();
PrintMsg(num1, 1);
num2 = Numbers();
PrintMsg(num2, 2);
return 0;
}
Thanks.