Thread: Checking input

  1. #1
    Unregistered
    Guest

    Checking input

    i am using this program to check input.
    Where can i put error message in this program to show user if the input is not integer
    cheers
    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]);

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    
    
    int isInteger( char [] );
    
    
    
    int main()
    {
    
    char input[100];
    
    printf("Give number: \n");
    scanf("%s", input);
    
    if(isInteger(input))
    printf("\n Yes");
    else
    printf("\n No");
    
    getch();
    
    return 0;
    } 
    
    
    
    
    int isInteger( char s[] ) {
    
    int i, len = strlen(s);
    
    if( len > 6)  //...FIXME : not a "failsafe" validation scheme...
    return 0;
    
    for(i = 0; i < len; i++ )
     {   
       if(!isdigit(s[i]))
       return 0;
     }
    
    return 1;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not just check the return code from scanf() and use an int variable.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	
    	if (scanf("%d", &i) == 1)
    		printf("You entered a number %d\n", i);
    	else
    		printf("You did not enter a number\n");
    	
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why not just check the return code from scanf() and use an int variable.
    Two good reasons:

    1) If the user enters a float value it will be truncated and all precision is lost when the program should flag an error for invalid input.
    2) If this code is placed in a loop as is and invalid input is entered all hell will break loose.

    With the problems in mind, and the work it would take to safely handle them, it's easier to transfer the number to an array and validate it that way.

    -Prelude
    My best code is written with the delete key.

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 for flag input through argv
    By cisokay in forum C Programming
    Replies: 6
    Last Post: 05-11-2005, 10:51 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  4. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM