View the step-by-step solution to:

# The program randomly chooses a number between 1 and 100 (included).

I need some help writing a program/ fixing my current code in C.Here is the assignment :

• The program randomly chooses a number between 1 and 100 (included).
• It then repeatedly prompts the player for a guess.
• An invalid input (that is not a number) is discarded and the player is prompted again.
• An invalid guess (not in the range 1 to 100) is also discarded and the player is prompted again.
• In both cases, the wrong input does not count towards the allowed number of tries.
• After each round, the program informs the player whether their guess was too small, too big, or equal to the number.
• The player has up to 10 tries to guess the number, in order to win the game.

Write program jojojeu.c which implements the "guess the number" game, as illustrated in the following example.

```What is your guess?
50
Your guess is too small! (9 tries left)
75
Your guess is too big! (8 tries left)
67
Your guess is too big! (7 tries left)
56
You guessed correctly in 4 tries!
```

Your program should be composed of five functions:

• A function that gets the random number that the player should later guess.
• In order to get a random value, you will have to include the headers stdlib.h and time.h, and initialize the random number generator by putting the following code inside your function:
```/* Initialize the random number generator */
srand(time(0));
```
• Once the random number generator is initialized, you can get a random value by adapting the following code accordingly:
```/* Get a random number between 0 and 9 into variable "r" */
r = rand() % 10;
```
• Note that it is not the best way of generating a random number (the distribution you get from using the modulo is not accurate), but it should be perfectly enough for this assignment.
• A function that prompts the player for a guess. This function should keep prompting the player until a valid integer, within the allowed bounds (1 to 100), is entered. The function should then return the guess.
• If the player supplies an invalid input (e.g. a character, or a floating-point number), scanf() will fail to return 1 (which would mean that it was able to read one integer number). In this case, we need to manually empty the input buffer.
• In order to empty the input buffer, you can use the following code:
```/* Eats the bad input until the newline character */
while (getchar() != 'n');
```
• This loop keeps reading characters one by one until a newline character is found.
• A function that receives as arguments the random number to be guessed, the guess that player has supplied and the current number of tries, and displays the status of the game.
• If the number was correctly guessed, the function should display the appropriate message and return 1.
• If the number was incorrect, the function should display whether the guess was too small or too big, as well as the number of tries left, and return 0.
• A function that receives as argument the number of current tries, and if it is greater than the number of allowed tries, displays that the player lost the game.

• Finally, the main() function should be the brain of the program and make use of all the other functions.
• It first gets a random number and then runs the game loop that gets a guess from the player and determines if this guess is correct or not.
• The game loop ends when the number is correctly guessed or when the number of tries exceeds the maximum allowed.

Here is what I have :

#include <time.h>

#include <stdlib.h>

#include <stdio.h>

void random_number_generator(int);

int input_check(int, char);

int compare_numbers(int, int, char, int, int );

void number_of_tries(int, int, int, int);

int main(void)

{

int random_number, user_guess, i=0, count=0, a;

char character;

random_number_generator(random_number);

input_check(user_guess, character);

compare_numbers(user_guess, random_number, character, i, count);

return 0;

}

void random_number_generator(int random_number){

srand(0);

random_number = rand() % 100 + 1;

}

int input_check(int user_guess, char character){

scanf("%d%c", &user_guess, &character);

if (character != 'n'){

printf("Invalid input, try again.n");

while (getchar() != 'n'){

scanf("%d%c", &user_guess, &character);

}

} else if ( user_guess >= 100 || user_guess < 1){

printf("Guess out of bounds, try again.n");

while(getchar() >= 100 || getchar()<1){

scanf("%d%c",&user_guess, &character );

}

}

return user_guess;

}

int compare_numbers(int user_guess, int random_number, char character, int i, int count){

number_of_tries(i, count, user_guess, random_number);

if (user_guess > random_number){

printf("Your guess is too big! (%d tries left)nWhat is your guess?n", count);

scanf("%d%c",&user_guess, &character );

} else if ( user_guess < random_number){

printf("Your guess is too small! (%d tries left)nWhat is your guess?n", count);

scanf("%d%c",&user_guess, &character );

} else if (user_guess == random_number){

printf("You guessed correctly in %d tries!", count);

}

return 0;

}

void number_of_tries(int i, int count, int user_guess, int random_number){

i = 10;

while ( user_guess != random_number && count <= 10) {

count = i - 1;

i--;

}

}

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents