input checking

This is a discussion on input checking within the C Programming forums, part of the General Programming Boards category; hi i need to check that correct input is given to my prog. i need to input a student number ...

  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,796
    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, 04:42 PM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 04:06 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 10: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

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