correct answer does not end the game
Okay so I thought I had this assignment completed properly last week. Last night I found a bug while playing the game.
My question is why won't the game end when the player guesses the correct number? The game allows you to finish using the max number of guesses even though you already guessed the correct number.
Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXGUESSES 6
#define MAXGAMES 20
#define MAXNUMBER 100
void MakeFile();
int Play(int answer);
int GetGuess();
int CompareGuess(int guess, int answer);
int main()
{
int i = 0;
int gamesToPlay = 0;
int answer = 0;
FILE *inp;
printf("\n\n\t\t\t\t****************\n\t\t\t *\t *\n\t\t\t\t*\t *\n\t\t\t\t* **** **** *\n\t\t\t\t* * * * * *\n\t\t\t\t* **** **** *\n\t\t\t\t* ** *\n\t\t\t\t* ** *\n\t\t\t\t*\t *\n\t\t\t\t* ****** *\n\t\t\t\t* ****** *\n\t\t\t\t*\t *\n\t\t\t\t****************\n\n\t\t\t\t HELLO!!\n\n\t\t\t Welcome to NUMBER GUESS");
printf(" \n\n\t\t\t My name is THE GENERATOR\n\n\n Instructions:\n\n\t* First you need to tell me how many games you want to play\n\n\t* You will then be prompted to start making guesses for your first game\n\n\t* When you guess the correct number the game is over\n\n\t* You only have 6 chances per game to guess the correct number\n\n\n");
MakeFile();
inp = fopen("answers.txt", "r");
printf(" Please enter the number of games to play (1 - 20 games).\t");
scanf(" %d", &gamesToPlay);
if(gamesToPlay < 2)
printf("\n\n\t\t\t\tLets Play!!\n\n");
else
printf("\n\n\t\t\t\tLets play game 1!!\n\n");
printf("\tremember you only have 6 guesses to get the correct answer\n\n");
for( i = 0; i < gamesToPlay; i++)
{
fscanf(inp, " %d", &answer);
Play(answer);
// *!* check the result of the function for a win or loss
fclose(inp);
return 0;
}
int Play(int answer)
{
int numGuesses = 0;
int guess = 0;
do
{
guess = GetGuess();
CompareGuess(guess, answer);
numGuesses = numGuesses +1;
}
while( numGuesses < MAXGUESSES);
return 0;
}
void MakeFile()
{
int i = 0;
int num = 0;
FILE *inp;
inp = fopen("answers.txt", "w");
srand(time(NULL));
for(i = 0; i < MAXGAMES; i++)
{
num = rand()%MAXNUMBER + 1;
fprintf(inp, "%d", num);
fclose(inp);
}
return;
}
int GetGuess()
{
int guess;
printf("\nPlease guess a number between 0 and 100: \n\t\t\t\t\t");
scanf(" %d", &guess);
return guess;
}
int CompareGuess(int guess, int answer)
{
if(guess == answer)
{
printf("Correct, %d is the right guess and you have won.\n", guess);
return 1;
}
if (guess > answer)
{
printf("%d is too high of a guess, you should have guessed lower\n", guess);
}
if (guess < answer)
{
printf("%d is too low of a guess, you should have guessed higher\n", guess);
}
return 0;
}