Like Tree2Likes
  • 2 Post By Matticus

correct answer does not end the game

This is a discussion on correct answer does not end the game within the C Programming forums, part of the General Programming Boards category; Okay so I thought I had this assignment completed properly last week. Last night I found a bug while playing ...

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

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

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Code:
    int Play(int answer)
    {   
        int numGuesses = 0; 
        int guess = 0;
         
         
        do
        {   
            guess = GetGuess();
             
            CompareGuess(guess, answer);
             
            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.
    CASHOUT and iMalc like this.

  3. #3
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Thank you very much Matticus, the game works good now.

  4. #4
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by CASHOUT View Post
    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 Play(int answer);
    int GetGuess();
    int CompareGuess(int guess, int answer);
    
    /* *************************************************************************** */
    
    int main()
    {
    	
    	int i = 0;
    	int gamesToPlay = 0;
    	int answer = 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();
    			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	
    	
    		if (guess == 1 && gamesToPlay > 1)
    		
    			printf("\t\t\t YOU WIN\nGAME 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);
    	return 0;
    }
    
    /* *************************************************************************** */
    
    int Play(int answer)
    {	
    	int numGuesses = 0;	
    	int guess = 0;
    	
    	
    	do
    	{	
    	
    		guess = GetGuess();
    		
    		
    		CompareGuess(guess, answer);
    		if(guess == answer)
    			break;
    		
    		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 = 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.\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;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    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()".

  7. #7
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Matticus View Post
    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;
    }

  8. #8
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    Smile

    Thanks for the help. Even though I didn't resolve the problem the way I originally wanted to, it did get resolved.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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;
    }
    
    
    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;
    }
    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

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    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;
    }
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 11:12 PM
  2. I have answer correct?
    By nutzu2010 in forum C Programming
    Replies: 9
    Last Post: 02-26-2011, 09:23 AM
  3. Are friend functions the correct answer here?
    By JimFromTexas in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2010, 05:25 PM
  4. my answer correct or not??
    By LINUX in forum C++ Programming
    Replies: 6
    Last Post: 10-22-2007, 06:51 AM
  5. Help me correct game errors (dos)
    By Mech0z in forum Game Programming
    Replies: 0
    Last Post: 02-11-2006, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21