So im having issues with my code using C. Havent learned how to use do/while commands yet. I never can see to get the final to be correct. I tried coding to display the random (9 for example) i press 9 and says too low. it then at the end the random number says is 14. I'm not sure how get the random number consistent throughout the user's input attempts.
// This is a guessing game. You have 4 attempts to guess the number between 1-20.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int usersInput;
int range;
range = rand() %21;
printf("Hello and welcome to my game! I have a number in my head between 1-20.\n");
printf("Try and guess what it is.\n");
printf("Please enter your guess as an integer.\n");
scanf("%d", &usersInput);
// These are the cases for your first attempted
// Attempt 1
if (usersInput > range) {
printf("Your guess is too high. Please try again\n");
}
else if (usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if (usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 2
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. Please try again\n");
}
else if(usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 3
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. Please try again\n");
}
else if(usersInput < range){
printf("Your guess is too low. Please try again\n");
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
return 0;
}
// Attempt 4
scanf("%d", &usersInput);
if (usersInput > range){
printf("Your guess is too high. You lose!\n");
printf("the random number is %d \n", rand() %21);
}
else if(usersInput < range){
printf("Your guess is too low. You Lose!\n");
printf("the random number is %d \n", rand() %21);
}
else if(usersInput == range){
printf("You are correct! Congratulations, you win!!\n");
printf("the random number is %d \n", rand() %21);
return 0;
}
}