correct answer does not end the game

• 02-21-2013
CASHOUT
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 GetGuess();

int main()
{
int i = 0;
int gamesToPlay = 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();
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++)
{
// *!* check the result of the function for a win or loss

fclose(inp);
return 0;
}

{
int numGuesses = 0;
int guess = 0;

do
{
guess = GetGuess();

numGuesses = numGuesses +1;

}
while( numGuesses < MAXGUESSES);
return 0;
}

void MakeFile()
{
int i = 0;
int num = 0;

FILE *inp;
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;
}

{
{
printf("Correct, %d is the right guess and you have won.\n", guess);
return 1;
}

{
printf("%d is too high of a guess, you should have guessed lower\n", guess);
}

{
printf("%d is too low of a guess, you should have guessed higher\n", guess);
}

return 0;
}

• 02-21-2013
Matticus
Code:

int numGuesses = 0;
int guess = 0;

do
{
guess = GetGuess();

numGuesses = numGuesses +1;

}
while( numGuesses < MAXGUESSES);

"CompareGuess()" returns 1 upon a successful guess. So you need to take this into account in your "Play()" function.

If the return value of "CompareGuess()" is 1, then just break out of this loop.
• 02-21-2013
CASHOUT
Thank you very much Matticus, the game works good now.
• 02-21-2013
CASHOUT
Quote:

Originally Posted by CASHOUT
Thank you very much Matticus, the game works good now.

I'm having issues getting my program to check for a win or lose properly.
everything runs fine up to the very last step, which i have commented with // *!* check the result of the function for a win or loss.

Code:

#define _CRT_SECURE_NO_DEPRECATE
#define MAXGUESSES 6
#define MAXGAMES 20
#define MAXNUMBER 100

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void MakeFile();
int GetGuess();

/* *************************************************************************** */

int main()
{

int i = 0;
int gamesToPlay = 0;
int guess = 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();

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++)
{

// *!* check the result of the function for a win or loss

if (guess == 1 && gamesToPlay > 1)

printf("\t\t\t YOU WIN\nGAME OVER!!\n");
MakeFile();

if (guess != 1 && gamesToPlay > 1)

printf("\n\t\t\t YOU LOSE!!\n\n\t\t\tGAME OVER!!\n");
MakeFile();
}

fclose(inp);
return 0;
}

/* *************************************************************************** */

{
int numGuesses = 0;
int guess = 0;

do
{

guess = GetGuess();

break;

numGuesses = numGuesses +1;

}
while( numGuesses < MAXGUESSES);

return 0;
}

void MakeFile()
{
int i = 0;
int num = 0;

FILE *inp;
srand(time(NULL));

for(i = 0; i < MAXGAMES; i++)
{

num = rand()%MAXNUMBER + 1;

fprintf(inp, "%d", num);
fclose(inp);
}
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;
}

{
{
printf("Correct!!, %d is the right guess.\n\n", guess);
return 1;
}

{
printf("%d is too high of a guess, you should have guessed lower\n", guess);
}

{
printf("%d is too low of a guess, you should have guessed higher\n", guess);
}
return 0;
}

• 02-21-2013
std10093
Lines 42-58. You need brackets in the if statements I guess. Also, how do you expect guess to update its value after the player plays?????
You return something from the play function... But it seems that you have forgotten to collect it back in main..
• 02-21-2013
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()".
• 02-22-2013
CASHOUT
Quote:

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 GetGuess();

/* *************************************************************************************************** */

int main()
{
/* variable declaration/initialization */
int i = 0;
int gamesToPlay = 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".*/
{

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();

if (guess != 1 && gamesToPlay > 1)

printf("\n\t\t\t YOU LOSE!!\n\n\t\t\tGAME OVER!!\n");
MakeFile();
}

fclose(inp);    // close the file
return 0;
}

/* ********************************FUNCTION DEFINITIONS BELOW***************************************** */

{
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
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;

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;
}

{
{
printf("Correct!!, %d is the right guess.  YOU WIN!!\n\n", guess);
return 1;
}
{
printf("%d is too high of a guess, you should have guessed lower\n", guess);
}
{
printf("%d is too low of a guess, you should have guessed higher\n", guess);
}
return 0;
}

• 02-22-2013
CASHOUT
Thanks for the help. Even though I didn't resolve the problem the way I originally wanted to, it did get resolved.
• 02-22-2013
AndiPersti
Code:

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;
}

{
{
printf("Correct!!, %d is the right guess.  YOU WIN!!\n\n", guess);
return 1;
}
{
printf("%d is too high of a guess, you should have guessed lower\n", guess);
}
{
printf("%d is too low of a guess, you should have guessed higher\n", guess);
}
return 0;
}

Your indentation style is rather strange. I recommend reading through https://en.wikipedia.org/wiki/Indent_style and choose one of the common styles.

Bye, Andreas
• 02-25-2013
hk_mp5kpdw
Code:

void MakeFile()
{
/*variable declaration/initialization */
int i = 0;
int num = 0;

/* opens a file to save numbers to */
FILE *inp;

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;
}

Your fclose statement within the body of the for-loop means that only one number gets written to the file. You probably mean for that to go outside the loop body.