cmc/learn
My personal learning area.
clone: git clone https://gitbay.org/cmc/learn.git
1#include <time.h>
2#include <stdlib.h>
3#include <stdio.h>
4
5// Create separate function to get a random number between 0 - 100
6int get_rand() {
7 srand(time(NULL));
8 int r = rand() % 100;
9 return r;
10}
11
12int main() {
13 // Call function to get a random number between 0 - 100
14 int random_number = get_rand();
15 printf("Random number: %d", random_number);
16
17 // Set a qualifier to check if the user has guessed correctly
18 int user_success = 0;
19
20 // Create a loop to allow the user to guess as many times as they want
21 while (user_success == 0) {
22 // Read user input
23 char line[256];
24 if(fgets(line, sizeof line, stdin) != NULL)
25 {
26 // Convert user input to an integer
27 int user_input = atoi(line);
28
29 // Do the main comparison to determine if the guess is correct
30 if (user_input == random_number) {
31 user_success = 1;
32 printf("Congratulations! You solved the puzzle!");
33 } else {
34 printf("Sorry, that's not correct! Please try again:\n");
35 }
36 }
37 }
38
39 return 0;
40}