Checking an input lenth and content

This is a discussion on Checking an input lenth and content within the C Programming forums, part of the General Programming Boards category; I am trying to write a script that will only allow even inputs from 2 to 98. If the user ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Checking an input lenth and content

    I am trying to write a script that will only allow even inputs from 2 to 98. If the user inputs an incorrect value or characters it should ask them to input the values again.

    Can anybody help me with this?

    I know how to check the length or if the characters are correct but can not figure out how to do both.

    Thanks, Tom

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you know how to check the length and you know how to check the validity of the characters, why not do one check after the other?

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    I can but then if i ask them to input again after checking the second time I need to check again and so on untill i have a lot of repeating code...

    I think.

    Unless im thinking about it totaly wrong. I need to always ask and validate untill they input the correct value.

    Code:
    #include <stdio.h>
    
    int main( void )
    {
        char number_res[1000];
        int temp;
    
    
        printf( "enter number of resistors\n" );
        scanf( "%s", number_res );
    
    
    
        while(number_res[temp] < 48 || number_res[0] > 57 || number_res[1] < 48 || number_res[1] > 57){
                printf( "Please enter numbers \n" );
    
                printf( "enter number of resistors\n" );
                scanf( "%s", number_res );
            }
                  
        
        
    
        printf("Number was %d\n", atoi(number_res));
    
        return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have a number_res that needs to be even and between 2 and 98 inclusive, right? So something like
    Code:
    while (isOdd(number_res) || isNegative(number_res) || is100OrBigger(number_res))
    This then becomes a question of checking each condition. Note that the idea is you do all three checks with the same input before asking again.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Ah.

    Thanks!

    I was just trying to do somthing utterly pointless and making life much more difficult for myself.

    Thanks again.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    whity4420, please have a look at https://apps.sourceforge.net/mediawi...tle=Scanf_woes regarding scanf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

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