#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Check for multiple numbers while scanf


    Hi,
    i have the below piece of code.
    I am actually trying to get my input as a string, in order to check if all the chars are between char 0-9 (i want them all to be numbers)
    then i will convert to numerics.
    the issue is that is also picking the \n at the last time and returns 0.
    I have no idea why this happens.
    Can anyone help?
    Also, you think there could be a better solution instead of this?

    Code:
    char input[50];
    
    int getInput(){
    	int i=0;
    	printf("Give Input:");
    	fflush(stdout);
    		scanf("%s",input);
    		while(input[i]!='\n'){
    			if((input[i]<'0' || input[i]>'9') && input[i]!='\n' )
    				return 0;
    			
    			else
    				i++;
    		}
    	return 1;
    }
    
    int main() {
     int ret;
    	 do{
    		ret=getInput();
    	 }while(ret==0);
    
    	return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    200
    Rep Power
    4
    Why don't you use getchar(void) for input? Take input using a loop and when you encounter a '\n' add it to the array to mark an end-of-the-line. Take input and then iterate through the whole array till you encounter '\n' and check if there's any char which is not lying in between 48 and 57.

    There's nothing wrong with your current approach but it's just that you're not fully understanding the library-functions you're using; scanf for example, doesn't just add every character it gets from the stdin to the array you give it (which would be the case if you manually used getchar). In your own code:
    Code:
    scanf("%s",input);
    You're telling scanf to get the input as a string "%s".

    from the scanf(3) manpage

    s Matches a sequence of non-white-space characters; the next
    pointer must be a pointer to the initial element of a character
    array that is long enough to hold the input sequence and the
    terminating null byte ('\0'), which is added automatically. The
    input string stops at white space or at the maximum field width,
    whichever occurs first.

    And as most programmers know, a string in C is nothing but a NULL-terminated array of characters. So instead of checking for a newline ('\n') if you'd just check for NULL (i.e. '\0') then everything would be fine...
    Code:
    while(input[i] != '\0'){
            if ((input[i] < 48 || input[i] > 57) && input[i] != '\0')
    	    return 0;
    Last edited by hexman; December 6th, 2016 at 11:01 AM.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,785
    Rep Power
    4300
    Easiest to do a fgets() followed by strtol()
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BASE_10     10
    
    int main(void) {
        char buf[80];
        long num;
        char *ptr;
        int len;
        printf("Enter a number: ");
        fgets(buf, sizeof(buf), stdin);
        len = strlen(buf);
        if (len > 0) {
            buf[len - 1] = '\0';
        }
        else {
            printf("That's not a number\n");
            return -1;
        }
        num = strtol(buf, &ptr, BASE_10);
        if (*ptr == '\0') {
            printf("You entered a number: %ld\n", num);
        }
        else {
            printf("You didn't enter a number. You entered %s\n", buf);
        }
    
        return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo