Help on restricting input to a numbers only.

This is a discussion on Help on restricting input to a numbers only. within the C Programming forums, part of the General Programming Boards category; Below is the code I am using so far. Below code no problem but it was too long and less ...

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

    Help on restricting input to a numbers only.

    Below is the code I am using so far. Below code no problem but it was too long and less efficient.

    Code:
    #include <stdio.h>
    
        int main(void)
        {
    
            char dummy[50];
            int c_size[1], nFields;
    
            printf("Input class size (num of students): ", stdout);
    
                nFields = scanf("%d%1[^\n]", &c_size[0], dummy);
    
                while(1 != nFields || c_size[0] < 1)
                {
                    printf("Invalid class size. Please try again.\n");
                    fputs("\nInput class size (num of students): ", stdout);
                    fflush(stdout);
    
                    while(0 != scanf("%*[^\n]")); 
    
                    nFields = scanf("%d%1[^\n]", &c_size[0], dummy); 
                }
    
        }
    So, I wonder if anyone got a more good suggestion to restricting input to number only. Thanks you.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int num_students = -1;
    
    do
       { printf("Number of students  : ");}
    while (scanf("%d", &num_students) < 1 || num_students < 1);

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Very thanks you ~ But could you explain to me what the purpose of num_students = -1 ?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It presets the number of students to an error value to make sure the loop repeats if the scanf() conversion fails.

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

    Validate integer & character in a string?

    First please view the code below.

    Code:
    #include <stdio.h>
    
        int main(void)
        {
            char ID[9];
                   
            printf("Enter Student ID  : ");
            scanf("%[^\n]", &ID);
        }
    I was trying to validate the student ID which should only allow 5 digits and 4 alphabets. E.g. 01234ADIA . I try to find code on line but no luck so far, so is it possible using c# to validate this?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You're going to have to write your own code to examine the student ID and check for the proper format.

    Grab the thing as a string... check each position for correctness using isdigit() and isalpha()

    Of course the correct format still doesn't mean it's a valid student ID... at some point you will need to check it against a list of ID's actually issued.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <conio.h> 
    #include <stdlib.h>
    
    int main (void)
    {
      int i=0;
      char str[9];
      char a = 0;
    
        printf("ID: ");
        scanf( "%[^\n]", &str );
    
        if( isalpha(str[9]) == 1 ) {
    
            a++;
    
        }
         
        printf( "You entered %d alphabet\n", a);
        
    
      return 0;
    }
    Why I can't calculate number of alphabet by using above code? It always say 0?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up isalpha() in your C Library Documentation... See exactly what it does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input Numbers
    By bummielove in forum C Programming
    Replies: 2
    Last Post: 07-23-2011, 08:20 PM
  2. Help on restricting input to a numbers only (or text only)
    By BongoBob in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2006, 09:32 PM
  3. Input numbers using realloc
    By Horse22 in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 11:09 AM
  4. Restricting input to an edit control
    By bennyandthejets in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2003, 02:10 AM
  5. count numbers input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-10-2002, 10:48 AM

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