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.
 
 

41 lines
1.0 KiB

#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.");
else if (guess_num < rand_num)
printf("Too low.");
else {
printf("Good job! You won using only %d attempts!\n", attempts+1);
playing = false;
continue;
}
printf(" You have used %d out of %d tries\n> ", attempts+1, tries);
if (++attempts == tries) {
printf("Sorry, you lost. The number was %d\n", rand_num);
playing = false;
}
} while(playing);
return 0;
}