Originally Posted by
Matticus
Just use the same concept you used to solve the previous problem. Have "Play()" return a value other than zero to indicate success, and check for it in "main()".
I've tried implementing a couple different changes, but had no success. I have about 2 hours left before I have to submit the program. If anyone can help me understand my mistake, I would greatly appreciate it.
Code:
//preprocessor directives
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h> // for input/output
#include <time.h> // to use the internal system clock for random number generation
#include <stdlib.h> // to use the random number generator
#define MAXGUESSES 6
#define MAXGAMES 20
#define MAXNUMBER 100
//function prototypes
void MakeFile();
int Play(int answer);
int GetGuess();
int CompareGuess(int guess, int answer);
/* *************************************************************************************************** */
int main()
{
/* variable declaration/initialization */
int i = 0;
int gamesToPlay = 0;
int answer = 0;
int guess = 0;
FILE *inp;
/* graphic/instructions on how to play the game */
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(); // calls the function that creates a file of random numbers
inp = fopen("answers.txt", "r"); // opens that file that was created to read from it
printf(" Please enter the number of games to play (1 - 20 games).\t"); // asks the user for the number of games to play
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++) /* Play as many games as the user requested, which is stored in the variable "gamesToPlay".*/
{
fscanf(inp, " %d", &answer); //reads integer from the file
Play(answer); // execute the function to play one game
/* checks the result of the function for a win or loss */
if (guess == 1 && gamesToPlay > 1)
printf("\n\t\t\t YOU WIN!!\n\n\t\t\tGAME OVER!!\n");
MakeFile();
inp = fopen("answers.txt", "r");
if (guess != 1 && gamesToPlay > 1)
printf("\n\t\t\t YOU LOSE!!\n\n\t\t\tGAME OVER!!\n");
MakeFile();
inp = fopen("answers.txt", "r");
}
fclose(inp); // close the file
return 0;
}
/* ********************************FUNCTION DEFINITIONS BELOW***************************************** */
int Play(int answer)
{
int numGuesses = 0;
int guess = 0;
do
{
guess = GetGuess(); // ask the user to guess the number
CompareGuess(guess, answer);// check if the guess was correct or not
if(guess == answer)
return 1;
numGuesses = numGuesses +1; // increases number of guesses made so far
}
while( numGuesses < MAXGUESSES);
return 0;
}
void MakeFile()
{
/*variable declaration/initialization */
int i = 0;
int num = 0;
/* opens a file to save numbers to */
FILE *inp;
inp = fopen("answers.txt", "w");
srand(time(NULL)); // "seeds" the number generator to randomize
for(i = 0; i < MAXGAMES; i++)
{
// generate a random number between 1 and MAXNUMBER
// assign that number to num
num = rand()%MAXNUMBER + 1;
fprintf(inp, "%d", num); // writes the generated number to the file
fclose(inp); // close the file
}
return;
}
int GetGuess()
{
int guess = 0;
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. YOU WIN!!\n\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;
}