You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
996 B

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main() {
srand(time(NULL));
int rand_num = (rand()%100);
int tries = 5;
int attempts = 0;
int guess_num;
char guess[100]; // Can overflow this, but not the point for this talk
bool playing = true;
printf("Number guesser game\n");
printf("You have %d tries\n", tries);
printf("Please guess a number between 0 and 99\n> ");
do {
//Bad scanf
if (scanf("%s", guess) == 0) {
printf("Bad input\n> ");
continue;
}
guess_num = atoi(guess);
if (guess_num > rand_num)
printf("Too high\n> ");
else if (guess_num < rand_num)
printf("Too low\n> ");
else {
printf("Good job! You won using only %d attempts!\n", attempts+1);
playing = false;
continue;
}
if (++attempts == tries) {
printf("Sorry, you lost. The number was %d\n", rand_num);
playing = false;
}
} while(playing);
return 0;
}