I'm trying to learn C. I try also to code this program that is supposed to do :
The program chooses a random number between 0 and 100 and the user has to guess which integer was chosen. Each time the user proposes an integer, the program should print :
'High' if the guessing number is higher than the chosen random number, and the user has the possibility to try again by entering again a new value
'Low' if the guessing number is lower than the chosen random number, and the user has the possibility to try again by entering again a new value
'Exact' if the guessing number is equal to the chosen random number
Here is my code :
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int guess;
srand(time(NULL));
int x = rand() % 100;
//printf("Random value is %d \n", x);
printf("Guessing value: \n");
scanf("%d", &guess);
switch (guess)
{
case guess < x:
printf("High \n");
scanf("%d", &guess);
break;
case guess > x:
printf("Low \n");
scanf("%d", &guess);
break;
case guess == x:
printf("Exact");
break;
default:
break;
}
return 0;
}
Can someone see why do I get :
error: case label does not reduce to an integer constant
Other try but the srand in the if doesn't work:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int guess;
srand(time(NULL));
int x = rand() % 100;
//printf("Random value %d \n", x);
printf("Guessing value: \n");
scanf("%d", &guess);
if (guess < x)
{
printf("High \n");
scanf("%d", &guess);
}
else if (guess > x)
{
printf("Low \n");
scanf("%d", &guess);
}
else
{
printf("Exact");
}
return 0;
}