From 3ec668ac415d2d92b28e658b17d8932d0cbe2b3b Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Mon, 5 May 2025 18:09:27 -0500 Subject: initial commit --- c/guess.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 c/guess.c (limited to 'c/guess.c') diff --git a/c/guess.c b/c/guess.c new file mode 100644 index 0000000..e9ab24b --- /dev/null +++ b/c/guess.c @@ -0,0 +1,40 @@ +#include +#include +#include + +// Create separate function to get a random number between 0 - 100 +int get_rand() { + srand(time(NULL)); + int r = rand() % 100; + return r; +} + +int main() { + // Call function to get a random number between 0 - 100 + int random_number = get_rand(); + printf("Random number: %d", random_number); + + // Set a qualifier to check if the user has guessed correctly + int user_success = 0; + + // Create a loop to allow the user to guess as many times as they want + while (user_success == 0) { + // Read user input + char line[256]; + if(fgets(line, sizeof line, stdin) != NULL) + { + // Convert user input to an integer + int user_input = atoi(line); + + // Do the main comparison to determine if the guess is correct + if (user_input == random_number) { + user_success = 1; + printf("Congratulations! You solved the puzzle!"); + } else { + printf("Sorry, that's not correct! Please try again:\n"); + } + } + } + + return 0; +} -- cgit v1.2.3-70-g09d2