This simple C program implements a classic Number Guessing Game where the user needs to guess a randomly generated number between 1 and 100. The program provides feedback to the user after each guess, indicating whether the guess is too high or too low. The game continues until the user correctly guesses the random number.
- The program generates a random number between 1 and 100 using the
getRandomNumberfunction. - The user is prompted to enter a guess using the
getUserGuessfunction. The input is validated to ensure it is a valid number within the specified range. - The program provides feedback on whether the guess is too high or too low.
- Steps 2 and 3 are repeated until the user correctly guesses the random number.
- Upon a correct guess, the program congratulates the user and displays the total number of attempts.
This function initializes the random number generator using the current time and returns a random number between 1 and 100.
This function prompts the user to enter a number between 1 and 100, validates the input, and ensures it falls within the specified range. It continues to prompt the user until a valid input is provided.
The main function initializes the game by generating a random number and then enters a loop where the user is prompted to guess the number. Feedback is provided after each guess until the correct number is guessed. The program then congratulates the user and displays the total number of attempts.