summaryrefslogtreecommitdiff
path: root/c/guess.c
blob: e9ab24ba7399387137113189cb06c59fa632fd28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}