#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/******************************
*Takes no arguments.
*Returns user guess as int.
******************************/
int takeUserGuess() {
int guess;
printf("input your guess: ");
if(scanf("%d",&guess) == 1) {
printf("correct input \n");
} else {
printf("incorrect input, expect an integer\n");
}
while(getchar() != '\n');
return guess;
}
/****************************
*Takes two arguments,
*guess and solution.
*
*Compares guess and solution,
*returns an int.
*
*0 if guess < solution,
*1 if guess == solution,
*2 if guess > solution,
*****************************/
int checkUserGuess(int guess, int solution) {
if(guess < solution) return 0;
if(guess == solution) return 1;
else return 2;
}
int main() {
srand(time(NULL));
int solution = rand() % 101;
int attempts = 0;
/************
* set to 1 if user guessed
* leave at zero otherwise
***********/
int isCorrect = 0;
while(attempts < 5) {
int guess = takeUserGuess();
int result = checkUserGuess(guess, solution);
attempts++;
if(result == 0) {
printf("too low!\n");
} else if(result == 1) {
printf("you guessed correctly!\n");
isCorrect = 1;
break;
} else {
printf("too high!\n");
}
}
if(isCorrect == 0) {
printf("unfortunately, you didn't guess correctly ;c, better luck next time!");
} else {
printf("wow! you guessed correctly! YIPPEEE!");
}
return 0;
}
simple game where you guess a random number
