Thread: help for a newb?

  1. #1
    Registered User
    Join Date
    Nov 2008

    help for a newb?

    I'm a newbie to C. I need a bit of help developing a C program that will fulfil the following requirements:

    - User interface to start a new game and quit the current game;
    - User interface to read the questions and select the answers for guessing animal names;
    - Algorithms to generate questions, make guesses and decide when the game has ended
    - Records and displays the number of questions asked and the time elapsed for each game

    The following code is an incremental step towards the final program. The algorithm tries to guess the users chosen integer between -100 and 400 in less than 20 guesses:

    /* This tells the compiler that the program will use source code defined in the standard C library. */
    #include <stdio.h>
    #include <string.h>
    /* These are convenient macros.
       The compiler replaces all instances of the macro name (all capitals) with the corresponding values */
    #define MINIMUM_NUMBER  -100
    #define MAXIMUM_NUMBER  400
    #define CONTINUE_KEY    "g"
    #define YES_KEY         "y"
    #define HIGH_KEY        "+"
    #define LOW_KEY         "-"
    #define BUFFER_SIZE     32
    /* Start of the C program */
    int main( void )
      int guess = 0;
      int counter = 0;
      int minimum_value = MINIMUM_NUMBER;
      int maximum_value = MAXIMUM_NUMBER + 1;
      char buffer[ BUFFER_SIZE ];
        printf( "\n" );
        printf( "Pick a number between %d and %d. ", MINIMUM_NUMBER, MAXIMUM_NUMBER );
        printf( "Press '%s' when you're ready and I will try to guess your number : ", CONTINUE_KEY );
        scanf( "%s", buffer );
      } while ( strncmp( buffer, CONTINUE_KEY, 1 ) != 0 );
        guess = minimum_value + ( ( maximum_value - minimum_value ) / 2 );
        printf( "\n" );
        printf( "Attempt %d: Is your number %d? ", counter, guess );
        printf( "Type '%s' if this guess is correct, '%s' if too high or '%s' if too low : ", YES_KEY, HIGH_KEY, LOW_KEY );
        scanf( "%s", buffer );
        if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
          printf( "\n" );
          printf( "It took %d attempts to guess your number, %d!", counter, guess );
          printf( "\n" );
        if ( strncmp( buffer, HIGH_KEY, 1 ) == 0 )
          maximum_value = guess;
          minimum_value = guess;
      } while ( 1 );
      /* Unix convention is to return zero from main for normal program exit */
      return 0;

    Firstly, I need to modify the code so that the program will stop guessing and exit after 20 guesses, displaying a message "cannot guess integer < 20 guesses".
    Would something
    Any help is much appreciated !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Already answered on tek-tips.

    It's polite to actually wait a while to see if any given forum delivers, rather than trolling the net looking for places to post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. newb question
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2008, 09:44 AM
  3. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM