Ctype.h problem?

This is a discussion on Ctype.h problem? within the C Programming forums, part of the General Programming Boards category; I want to make sure that my user only enters a numbered response and if not then it goes back ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Ctype.h problem?

    I want to make sure that my user only enters a numbered response and if not then it goes back to asking the main question. I know goto is out of the question and duplicity of any kind is sloppy code. So how would i go about doing this please. I think i need somehting in the ctype.h right?

    Code:
             /* guessing game program
    * (c) 2003 practical applications
    
    * To be refactored including:
    
    * user input validation on scanf()
    
    * calling only one function in main()
    
    * preprocessor directives validating and executing platform
      specific code
    
    * srand to generate better random numbers */
    
    /* the game */
    
    
    #include <stdio.h>
    #define RUNNING 1
    
    
    
    int main()
    
    {
        int number;int guess;int tries = 0;
        char name[15];
    
    
        system("TITLE Stupid guessing game");
        printf("Hey, whats your name?");
        gets(name); /* in case it is alpha numeric */
    
        system("cls");
        printf("Hey %s, we are gonna play a game\n\n", name);
        system("pause");
        system("cls");
        system("COLOR 74");
    
    
    
        while(RUNNING)
                {
                number = rand() % 100 + 1;
                printf("guess a number between 1 and 100: ");
                gets(guess);  /* works better but still have a warning */
                tries++;
            
                if(guess > number)
    
                    printf("too high. The number was %d\n", number);
                    
                    else if(guess < number)
                    printf("too low. The number was %d\n", number);
            
                    else if(guess == number)
            		
                    printf("perfect you only need %d tries to guess!\007\007\007\n", tries);
    
                 }
    
    
            
    
    
    return 0;
    system("PAUSE");
    
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use fgets( ) and then sscanf( ), and then just check the return code from sscanf( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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