Thread: 20q game problems

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok this is what ive come with so far, i no its not complete but tell if im going in the right direction or not

    Code:
      if ( buffer == 'y' || buffer == 'n' || buffer == 'q' );
      {
      else
    	  printf ("\nInvalid! Please choose either YES, NO or QUIT.")
      
      }
    This is what im gonna put in the askUserQuestion function.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is buffer a char-array or a char?

    --
    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.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Its just a char

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, then there's only the problem that you have a semicolon immediately after the end of the expression in the if-statement and some misplaces braces that will prevent the code from compiling (as you end up with a "unexpected else" or some such).

    --
    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.

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok i got some errors, it seems that my if statement is making an comparison between an pointer and an integer.
    Also the 'askUserQuestion()' has to few arguments.

    Code:
      if ( buffer == 'y' || buffer == 'n' || buffer == 'q' )
      {
    		askUserQuestion();
      }
      else
      {
    	  printf ("\nInvalid! Please choose either YES, NO or QUIT.");
      }

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "comparison between integer and pointer" is usually caused by:
    Code:
    char buffer[X];
    
    if (buffer == 'A') ...
    Now, 'A' is the same as 65 [assuming we are using the ASCII/ANSI character set - other character sets use different encodings, but the result is the same], and buffer is the address (in other words a pointer to) the first character in buffer - so you are checking if the memory used for buffer is at location 65 in memory - which the compiler finds to be an unlikely thing to do and gives a warning. It is perfectly LEGAL to make such a comparison, and it may even be the right thing at times - but most fo the time, it's completely wrong, and if your code looks something like the above, then you are definitely not comparing what you think you are comparing.

    Perhaps you want to compare the first character of the buffer, by comparing buffer[0] with the relevant posisble answers?

    Also, if this happens to be inside
    Code:
    void askUserQuestion( int guessNumber, char* question, int* animalData )
    , then the code will crash if someone happens to put a book on the enter key - because EVENTUALLY you will run out of stack. I also don't think it's actually the right thing to do regardless of whether it tolerates "book on enter key" accidents, because you have no way of skipping back to where you came from within the askUserQuestion() and the one that you started from - so if the user answers wrong three times in a row, you will be three levels into the askUserQuestion() function, but when you go back, you will go back to the previous level and perform whatever operations it does after returning from the previous call.

    By the way, if you still have this:
    Code:
    	  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;
    	  }
    in your askUserQuestion(), then you could simplify it:
    Code:
    int selectionCriteria; 
    	  if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
    	  {
                   selectionCriteria = 0;
              }
              else
              {
                   selectionCriteria = 1;
              }
              if ( animalData[ index ] == selectionCriteria )
              {
    		  animals_list[ index ] = NULL;
              }
    That way, you don't repeat essentially the same test in two places.

    --
    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.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Ok i did that but it still gives me the same warning msg of a comparison between an pointer and an integer, and wont let me compile.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nexus-ZERO View Post
    Ok i did that but it still gives me the same warning msg of a comparison between an pointer and an integer, and wont let me compile.
    You probably need to post the code for the function that gives you problems.

    --
    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.

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    26
    Oh yeah sorry about that

    Code:
    void askUserQuestion( int guessNumber, char* question, int* animalData )
    {
      
      char buffer[ BUFFER_SIZE ];
      int  index;
      int selectionCriteria; 
     
      printf( question, guessNumber );
      printf( "Type '%s' if yes '%s' if no and type '%s' if you want to quit: ", YES_KEY, NO_KEY, EXIT_KEY );
    
      /* Wait for the user answer */
      scanf( "%s", buffer );
    
    if ( buffer == 'y' || buffer == 'n' || buffer == 'q' ){
    
    	}
      
      else
      {
    	  printf ("\nInvalid! Please choose either YES, NO or QUIT.");
      }
      
      /* 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 )
    			{
                   selectionCriteria = 0;
    			}
            else
                {
                   selectionCriteria = 1;
                }
            if ( animalData[ index ] == selectionCriteria )
                {
    		  animals_list[ index ] = NULL;
                }
    
    	  
    	}
      }
    }

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are still checking if your buffer is located at memory address 'y', 'n' or 'q' - which is VERY UNLIKELY to be the case, and the reason the compiler is complaining. See my previous post for one possible solution for that.

    And your indentation for the new if/else bits is completely out of whack - probably because of a mix of tabs and spaces.

    --
    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.

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