diff options
author | Christian Cleberg <hello@cleberg.net> | 2025-05-05 18:09:27 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2025-05-05 18:09:27 -0500 |
commit | 3ec668ac415d2d92b28e658b17d8932d0cbe2b3b (patch) | |
tree | 7bfb32b5b0d5731e9d9468809b3b0cdd1e744201 /c/guess.c | |
download | learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.tar.gz learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.tar.bz2 learn-3ec668ac415d2d92b28e658b17d8932d0cbe2b3b.zip |
initial commit
Diffstat (limited to 'c/guess.c')
-rw-r--r-- | c/guess.c | 40 |
1 files changed, 40 insertions, 0 deletions
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 <time.h> +#include <stdlib.h> +#include <stdio.h> + +// 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; +} |