Thread: input checking

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    input checking

    hi

    i need to check that correct input is given to my prog.

    i need to input a student number containing only 6 integers (e.g. 123456) and nothing else
    in case something else is typed in the prog should just repet the request for a 6 integer string or something like that

    how do i do this in c?

    thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    int digits, copy = input;
    for ( digits = 0; copy != 0; copy /= 10 )
      ++digits;
    if ( digits != 6 )
      printf ( "Error: Invalid input\n" );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    char input[100];
    int i;
    do
    {
        printf("Give number: ");
        scanf("%s", input);
        for(i = 0; input[i] && isdigit(input[i]); i++) ;
    } while(i != 6 || input[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 09:00 AM
  4. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM