-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
47 lines (38 loc) · 1.17 KB
/
main.c
File metadata and controls
47 lines (38 loc) · 1.17 KB
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
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void init () {
setvbuf(stdout, 0, 2, 0);
setvbuf(stdin, 0, 2, 0);
setvbuf(stderr, 0, 2, 0);
}
int main () {
init();
srand(time(0)); // Initialize random number generator
int secret_number = rand() % 100 + 1; // Secret number between 1 and 100
int guess;
char buf[64];
int len = 0;
printf("Welcome to the guessing game!\n");
printf("I have selected a number between 1 and 100. Can you guess it?\n");
while (1) {
printf("Enter the length of your guess (max 64): ");
scanf("%d\n", &len);
if (len < 0) len = abs(len);
if (len > 64) len = 64;
printf("Enter your guess: ");
read(0, buf, len);
guess = atoi(buf);
if (guess < 1 || guess > 100) {
printf("Please enter a number between 1 and 100.\n");
} else if (guess < secret_number) {
printf("Too low! Try dgdLu.\n");
} else if (guess > secret_number) {
printf("Too high! Try again.\n");
} else {
printf("Congratulations! You guessed the correct number.\n");
break;
}
}
return 0;
}