Thread: 20q game problems

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    26

    20q game problems

    Hi everyone, ive got a couple of problems with my code. I trying to add functinality to my program but it isnt going well.
    What i want to do is to be able to quit or restart my game at anytime. I thought i could do a strcmp between the buffer and my exit_key, but so far have been unsuccessful.
    Can anyone help?

    edit:sorry if this is the wrong forum, i just noticed the game one!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    /*
     * List of animals that the user can select for the game.
     * Below the animal names are characteristics for each animal listed in the same order.
     * Each entry should be separated by a space and spelled in all lowercase.
     */
    #define ANIMALS_LIST      "giraffe trout tiger elephant jellyfish eagle piranha hummingbird parrot dolphin terns zebra chipmunk bass bluejay skunk wolf bear reindeer leopard ladybug robin outter caribou boar polarbear snowyowl rockptarimigan dalmation housecat"
    #define LIGHT_ANIMALS     "0 1 0 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1"
    #define CARNIVORES        "0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 1 0 1 1"
    #define HERBIVORES        "1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0"
    #define SWIMMING_ANIMALS  "0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0"
    #define FLYING_ANIMALS    "0 0 0 0 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0"
    #define FOREST_ANIMALS    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 0"
    #define JUNGLE_ANIMALS    "0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0"
    #define STRIPED_ANIMALS   "0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
    #define SPOTTED_ANIMALS   "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0"
    #define TUNDRA_ANIMALS    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0"
    
    /*
     * Each entry in the list is separated by a space.
     */
    #define ANIMAL_DELIMITER  " "
    
    /*
     * The total number of animals determines the amount of memory used by the program.
     */
    #define TOTAL_ANIMALS     30
    
    /*
     * Wait for user to type this before continuing with the game.
     */
    #define CONTINUE_KEY      "g"
    
    /*
     * The user must type this to indicate a yes answer.
     */
    #define YES_KEY           "y"
    
    /*
     * The user should type this to indicate a no answer.
     * But, any answer other than yes could be considered to be a no answer.
     */
    #define NO_KEY            "n"
    
     /*
    *The user should type this to indicate they want to quit
    */
    
    #define EXIT_KEY          "q"
    
    /*
     * Amount of characters for the user to type their answers.
     */
    #define BUFFER_SIZE       32
    
    /*
     * Turn off debugging output by setting this to zero.
     * Any non-zero value will turn debugging on.
     */
    #define DEBUG_STATUS      1
    
    
    /*
     * Global array to store all the animals in the list.
     */
    char* animals_list[ TOTAL_ANIMALS ] = { NULL };
    
    /*
     * Global array to store animals which live in the jungle (using the same index order).
     */
    int   jungle_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which are herbivores (using the same index order).
     */
    int   herbivores[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which swim (using the same index order).
     */
    int   swimming_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which fly (using the same index order).
     */
    int   flying_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which are light in weight (using the same index order).
     */
    int   light_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which are carnivores (using the same index order).
     */
    int   carnivores[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which are striped (using the same index order).
     */
    int   striped_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which live in forest areas (using the same index order).
     */
    int   forest_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which live in tundra areas (using the same index order).
     */
    int   tundra_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Global array to store animals which are spotted (using the same index order).
     */
    int   spotted_animals[ TOTAL_ANIMALS ] = { 0 };
    
    /*
     * Please see function definitions for description of function prototypes.
     */
    void  setupGameData();
    void  startGame();
    void  askUserQuestion( int, char*, int* );
    void  displayRemainingAnimals();
    int   countRemainingAnimals();
    char* findLastAnimal();
    
    /*
     * Start of the program.
     */
    int main( void )
    {
      /* Variable to indicate that the game should continue */
      int continue_game = 1;
    
      /* Initialize the number of attempts to zero */
      int guesses = 0;
    
      char buffer[ BUFFER_SIZE ];
      
      time_t start_time, end_time;
    
      setupGameData();
    
      startGame();
    
      /* Start timing the game session */
      start_time = time( NULL );
      printf( "\nGame started at &#37;s\n", ctime( &start_time ) );
    	
      /* Start the main loop to play the game */
      do
      {
    	
        /* Increment the attempts counter by 1 */
    	guesses++;
    
    	switch ( guesses )
    	{
    	  
    	  case 1:
    		askUserQuestion( guesses, "\nAttempt %d: Can you pic up your animal? ", light_animals );
    		break;
    	  case 2:
    		askUserQuestion( guesses, "\nAttempt %d: Is your animal a carivore? ", carnivores );
    		break;
    	  case 3:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal eat only plants? ", herbivores );
    		break;
    	  case 4:
    		askUserQuestion( guesses, "\nAttempt %d: Can your animal swim? ", swimming_animals );
    		break;
    	  case 5:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal fly? ", flying_animals );
    		break;
    	  case 6:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal live in forest areas? ", forest_animals );
    		break;
    	  case 7:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal live in jungle areas? ", jungle_animals );
    		break;
    	  case 8:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal have stripes? ", striped_animals );
    		break;
    	  case 9:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal have spots? ", spotted_animals );
    		break;
    	  case 10:
    		askUserQuestion( guesses, "\nAttempt %d: Does your animal live in tundra areas? ", tundra_animals );
    		break;
    
    	  default :
    		/* Losing end of game condition */
    		printf( "\nYou win, I'm out of ideas!\n" );
    
    		/* Stop the game */
    		continue_game = 0;
    		break;
    	}
    	
    	if ( DEBUG_STATUS )
    	  displayRemainingAnimals();
    	
    	
    	/* Winning end of game condition */
    	if ( countRemainingAnimals() == 1 )
    	{
    	  printf( "\nIt took %d attempts to guess your animal, %s!\n", guesses, findLastAnimal() );
    	
    	  /* Stop the game */
    	  continue_game = 0;
    	}
    
      } while ( continue_game );
    
      /* Stop timing the game session */
      end_time = time( NULL );
      printf( "\nGame ended at %s\n", ctime( &end_time ) );
    
      /* Unix convention is to return zero from main for normal program exit */
      return 0;
    }
    
    
    /*
     * setupGameData : Parses the list of animals and characteristics.
     */
    void setupGameData()
    {
      char* animal;
      int   index;
    
    /* Start the process to parse the list of animals and characteristics */
      animal = strtok( strdup( ANIMALS_LIST ), ANIMAL_DELIMITER );
    
    /* Go through the list of animals until the end of the list is found, indicated by a NULL value */
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	/* Assign the individual animal into the appropriate index */
    	animals_list[ index ] = animal;
    
    	/* Continue to the next animal */
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    
    /* Update characteristic for animals that are light in weight */
      animal = strtok( strdup( LIGHT_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	light_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      } 
    
    /* Update characteristic for animals that are carnivores */
      animal = strtok( strdup( CARNIVORES ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	carnivores[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    /* Update characteristic for herbivores */
      animal = strtok( strdup( HERBIVORES ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	herbivores[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
     
    /* Update charactertistic for swimming animals */
      animal = strtok( strdup( SWIMMING_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	swimming_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    
    /* Update characteristic for flying animals */
      animal = strtok( strdup( FLYING_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	flying_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      } 
    /* Update characteristic for animals that live in the forest */
      animal = strtok( strdup( FOREST_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	forest_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
      
    /* Update characteristic for animals that live in the jungle */
      animal = strtok( strdup( JUNGLE_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	jungle_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    /* Update characteristic for animals that are striped */
      animal = strtok( strdup( STRIPED_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	striped_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    
    /* Update characteristic for animals that are spotted */
      animal = strtok( strdup( SPOTTED_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	spotted_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
      
    /* Update characteristic for animals that live in the tundra */
      animal = strtok( strdup( TUNDRA_ANIMALS ), ANIMAL_DELIMITER );
      for ( index = 0; ( index < TOTAL_ANIMALS ) && ( animal != NULL ); index++ )
      {
    	tundra_animals[ index ] = atoi( animal );
    	animal = strtok( NULL, ANIMAL_DELIMITER );
      }
    }
    
    
    /*
     * startGame : Display game instructions and list of available animals for the user to select.
     *             If debug is on, also display the list of animals and characteristics.
     */
    void startGame()
    {
      char buffer[ BUFFER_SIZE ];
      int  index;
      
      do
      {
    	printf( "\nPick one of the following animals:\n\n" );
    	for ( index = 0; index < TOTAL_ANIMALS; index++ )
    	  printf( "%s ", animals_list[ index ] );
    	printf( "\n\nPress '%s' when you're ready and I will try to guess the animal: ", CONTINUE_KEY );
    	scanf( "%s", buffer );
    	
      } while ( strncmp( buffer, CONTINUE_KEY, 1 ) != 0 );
    
      if ( DEBUG_STATUS )
      {
    	/* Go through and display the list of animals and characteristics */
    	for ( index = 0; index < TOTAL_ANIMALS; index++ )
    	  printf( "%s: %d %d %d %d \n", animals_list[ index ], jungle_animals[ index ], herbivores[ index ], swimming_animals[ index ], flying_animals[ index ] );
      }
    }
    
    
    /*
     * askUserQuestion : Ask the user questions about the animal
     *
     * Parameters      : Number of current attempt
     *                   Text of the question
     *                   Array of data for that question
     */
    void askUserQuestion( int guessNumber, char* question, int* animalData )
    {
      char buffer[ BUFFER_SIZE ];
      int  index;
     
      printf( question, guessNumber );
      printf( "Type '%s' if yes '%s' if no : ", YES_KEY, NO_KEY );
    
      /* Wait for the user answer */
      scanf( "%s", buffer );
    
    
      /* Go through the list of remaining animals and take action based on the user answer */
      for ( index = 0; index < TOTAL_ANIMALS; index++ )
      {
    	/* Only process if there is an animal at that location in the array */
    	if ( animals_list[ index ] != NULL )
    	{
    	  if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
    	  {
    		/* If the user answer is yes, then remove animals do not match */
    		if ( animalData[ index ] != 1 )
    		  animals_list[ index ] = NULL;
    	  }
    	  else
    	  {
    		/* If the user answer is no, then remove animals which match */
    		if ( animalData[ index ] == 1 )
    		  animals_list[ index ] = NULL;
    	  }
    
    	  
    	}
      }
    }
    
    /*
     * displayRemainingAnimals : Display the list of possible animals remaining.
     */
    void displayRemainingAnimals()
    {
      int index;
    
      printf( "\nAnimals remaining: " );
      for ( index = 0; index < TOTAL_ANIMALS; index++ )
      {
    	if ( animals_list[ index ] != NULL )
    	  printf( "%s ", animals_list[ index ] );
      }
      printf( "\n" );
    }
    
    
    /*
     * countRemainingAnimals : Count the list of possible animals remaining.
     *
     * Returns the number counted.
     */
    int countRemainingAnimals()
    {
      int animals_remaining = 0;
      int index;
    
      for ( index = 0; index < TOTAL_ANIMALS; index++ )
      {
    	if ( animals_list[ index ] != NULL )
    	  animals_remaining++;
      }
    
      return animals_remaining;
    }
    
    
    /*
     * findLastAnimal : Find the last animal on the list.
     *
     * Returns the animal found.
     */
    char* findLastAnimal()
    {
      int index;
    
      for ( index = 0; index < TOTAL_ANIMALS; index++ )
      {
    	if ( animals_list[ index ] != NULL )
    	  return animals_list[ index ];
      }
    
      return NULL;
    }
    Last edited by Nexus-ZERO; 12-17-2008 at 06:31 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, your code doesn't attempt to do so currently, so it's hard to say why it doesn't work.

    As a side note:

    Code:
    #define ANIMALS_LIST      "giraffe trout tiger elephant jellyfish eagle piranha hummingbird parrot dolphin ..."
    #define LIGHT_ANIMALS     "0 1 0 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1"
    #define CARNIVORES        "0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 1 0 1 1"
    #define HERBIVORES        "1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0"
    #define SWIMMING_ANIMALS  "0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0"
    #define FLYING_ANIMALS    "0 0 0 0 0 1 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0"
    #define FOREST_ANIMALS    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 0"
    #define JUNGLE_ANIMALS    "0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0"
    #define STRIPED_ANIMALS   "0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
    #define SPOTTED_ANIMALS   "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0"
    #define TUNDRA_ANIMALS    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0"
    This seems a bit poor representation for the data. It isn't readable, it is hard to process (look at how many functions it takes), it is hard to modify/add data etc.

    Rather than encoding data in semi-human-readable strings you might try bitfields (basically the same idea, except the data is encoded as bits in a perfectly ordinary integer.

    E.g

    Code:
    enum properties {
        IsLight = 1 << 0, /*first bit*/
        Carnivore = 1 << 1, /*second bit*/
        Herbivore = 1 << 2, /*third bit*/
    /*etc for the rest of the properties, which can be in any order as long as they are powers of 2*/
    Then an animal might be set up as follows
    Code:
    typedef struct
    {
         const char* name,
         unsigned properties
    } Animal;
    
    /*create instances like this*/
    Animal zoo[MAX_ANIMALS] = {
        { "giraffe", Herbivore | Spotted }, /*use bitwise or to combine properties
        { "tiger", Carnivore | Feline | Spotted },
    /*etc*/
    };
    Testing if an animal has a particular property is a piece of cake (you can easily create a single simple function that filters out animals that have one property or another

    Code:
    if (zoo[n].properties & Herbivore) /*use bitwise and to test if the corresponding bit is set*/
    {
         /*animal represented by zoo[n] has the property Herbivore set*/
    }
    Of course, these are just suggestions but they should pay off.

    ------------

    Edit: alternatively you might represent each property as a zero-based list of integers

    Code:
    enum Properties
    {
         IsLight = 0,
         Carnivore, /* == 1*/
         Herbivore, /* == 2*/
    /*etc*/
    };
    That would make it easy to associate properties with an array of questions: the property would simply be an index into that array.

    However, to turn the property into a single-bit value to be used in bitfields, you might use the following macro or function

    Code:
    #define PROP(x) (1 << (x))
    ------------

    edit:sorry if this is the wrong forum, i just noticed the game one!
    I believe the reason is that in (graphical) games you'd typically use third-party sound, graphics etc libraries whereas language board prefer if the code is limited to using the standard headers.
    Last edited by anon; 12-17-2008 at 07:05 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Im only a beginner so i dont really understand most of what you have put there. I only need to know how to put in a quit function that i can activate anytime anywhere in my program.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You can call
    Code:
    exit (0);
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Yeah i looked at that, but its not what im after. I just want it so that when i press my exit key (q) the game just shuts down. I know its a loop i need, but i havent been able to make any work.

    something like

    Code:
    do{
          if (strcmp ( buffer, EXIT_KEY, 1) == 0)
             
             printf("\nThank you for playing");
             
             continue_game = 0;
             
             break;
    
    }while ( continue_game );

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You simply need an input prompt and statement somewhere in the loop. Also the loop should contain a call to a function that plays the entire game, so that the loop is around everything that the program does.

    I wonder how you could have possibly written all that code and not figure out this simple matter...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    LoL what are you trying to imply

    Listen i already knew all that, and ive tried to implement it but some reason all i get is errors which is why im here!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nexus-ZERO View Post
    LoL what are you trying to imply

    Listen i already knew all that, and ive tried to implement it but some reason all i get is errors which is why im here!
    You could have mentioned errors before. In fact, you could still mention what the errors actually are now.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could post code that actually attempts to do that, so that we could see what the errors might be (e.g repost the main function that actually does attempt to run the game in a loop).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Nevermind ive done it now.

    What i did was make a new function..............

    Code:
    int quitGame()
    {
    	int quit = 0;
    	char buffer[ BUFFER_SIZE ];
    	
    	if ( strcmp ( buffer, EXIT_KEY ) )
    	{
    		quit++;
    	}
    	
    	return quit;
    }
    Then i added this to the main function..........

    Code:
    	if ( quitGame() == 1 )
    	{
    		printf ("\nThank you for playing!");
    		
    		continue_game = 0;
    	}
    Now all this has led to a new problem, now when i start the program any key i press quits the game. Any ideas on how to stop this from happening?

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If that is the exact code, I don't see how comparing against an uninitialized buffer can do any good...

    As to why the program doesn't work - how should we know without code!

    It's perfectly straight-forward, though

    Code:
    do {
         play_a_game();
    } while (!user_wants_to_quit());
    Last edited by anon; 12-17-2008 at 11:09 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    However, quitgame just compares some random data held in "buffer" to the EXIT_KEY value. Highly unlikely to ever become true (random in this case isn't random in the sense that it's a good source for random numbers, but rather random in the sense that it is not known, and will vary depending on a number of different circumstances - but it's highly likely to never be EXIT_KEY).

    Code:
    quit++;
    Perhaps you'd like to say
    Code:
    quit = 1;

    Also, strcmp returns 0 for a match, and other numbers for non-equal result, so it's very likely that you ALWAYS return 1 from this function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    K i understood that, so how would i go about making it so that wat is in is true?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could maybe not use random uninitialized data, but let the user type something in.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    You could maybe not use random uninitialized data, but let the user type something in.
    Or, perhaps add it to the "ask yes or no" and add "or 'q' to quit" there, so that a user can quit at any time.

    Edit: Speaking of which, if the user types in ANYTHING ELSE besided "y", the answer will be considered "no" as it stands - it may be better to say something like "Please answer yes or no, try again", instead of assuming that anything that isn't yes must be "no".

    --
    Mats
    Last edited by matsp; 12-17-2008 at 12:07 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  3. Game Problems
    By Spectrum48k in forum Tech Board
    Replies: 4
    Last Post: 06-02-2004, 07:08 PM
  4. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  5. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM