#include <stdio.h> #include <stdlib.h> #include <time.h> // Brandon Treno int compare(int guess, int target) { // set message char* str = "Correct!"; int res = 1; if (guess > target) { str = "<Too High"; res = 0; } if (guess < target) { str = "<Too Low"; res = 0; } printf("%s", str); return res; } int main() { // initialize random seed srand(time(NULL)); char cont = 'y'; while (cont == 'y' || cont == '1') { // initialize int guess = -1; int myNum = rand() % 100 + 1; int attempt = 0; // intro printf("Welcome to Guess My Number!\n"); printf("===========================\n"); // 5 attempts while (attempt < 5 && attempt >= 0) { scanf("%d", &guess); //debug printf("%-3d %3d ", myNum, guess); // if guess is correct (also display comparison) if (compare(guess, myNum) > 0) { // skip to end of attempts // since answer is correct attempt = 100; } else { // print attempts remaining printf(" - %d more attempts>", 4-attempt); } printf("\n"); attempt++; } // play again printf("\nWould you like to play again? "); // default to no cont = 'n'; scanf("%s", &cont); printf("%c\n\n", cont); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more