Thread: Validate integers helps ~

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Validate integers helps ~

    Code:
    #include <stdio.h>
    #include <ctype.h>
      
        int main(void)
        {
            int size, i;
    
            do {
    
                printf("Enter class size (<50): ");
                scanf("%d", &size);
    
            } while(!isdigit(size));
    
            for (i=0; i <size; i++)
            {
            
            }
            
      
            return 0;
        }
    Why my code can't validate integers? Very thanks you.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to check the returned value from scanf() to determine if the conversion failed or not...
    Presetting your variable to an impossible value also helps...
    Code:
    do
      { size = 0;
        printf("Enter class size (1 to 50) : "); }
    while( (scanf(" %d",&size)  < 1) || (size < 1) || (size > 50) );
    Look up scanf() in your C Library Documentation for more information. (If you don't have it, get it...)
    Last edited by CommonTater; 11-30-2011 at 07:02 AM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Thanks ~ But could you tell me where could I found the c documentation? Because I never view before so I don't know where to search for it...this sound noob but....

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Go to the home page for your compiler... You should be able to download compiler and library specific documentation there.

    If your compiler doesn't have it... get another compiler.

    Pelles C, comes with help files that contain the full documentation. (In POIDE all you do is put your text cursor on a keyword and press F1.)

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Or go to Google and type in "man <whatever the function name is>"

    You have the entire Internet at your disposal...use it!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Or go to Google and type in "man <whatever the function name is>"

    You have the entire Internet at your disposal...use it!
    I don't disagree... but it's always preferable to have the exact compiler specific documentation whenever possible.

    Going back to the Pelles C example... there are several hundred non-standard functions available in various libraries that you would never know about without the help files Pelle provides.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c/c++ programming nid helps!!
    By thankyou in forum C Programming
    Replies: 5
    Last Post: 09-25-2011, 10:05 AM
  2. Somebody please helps me!
    By hchingwong in forum C Programming
    Replies: 3
    Last Post: 09-23-2010, 08:59 PM
  3. Helps with strings
    By 3wit in forum C Programming
    Replies: 3
    Last Post: 05-01-2008, 07:58 AM
  4. Here's a problem, any one helps me?
    By NightWalker in forum C Programming
    Replies: 19
    Last Post: 06-10-2003, 09:19 AM