user1814928
user1814928

Reputation: 23

Scanf for digits

I am pretty new in C and I have a question about scanf just for digits. What I need to do is scanf in input just 3 digits, antoher characters or symbols should be evaluate as trash. Or maybe I need use isdigit() but I am not sure how it works. I have just that, but I know that it doesn't work:

scanf("%d, %d, %d", &z, &x, &y);

Upvotes: 2

Views: 10059

Answers (3)

puyu
puyu

Reputation: 1

Try this.

If the first char is not a digit. Use "%*[^0-9]" to skip chars which is not digits.

' * ' is an optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument), and ' ^ ' means any number of characters none of them specified as characters between the brackets.

#include <stdio.h>

int main()
{
    int x,y,z;
    if(!scanf("%d",&x)==1) scanf("%*[^0-9] %d",&x);
    if(!scanf("%d",&y)==1) scanf("%*[^0-9] %d",&y);
    if(!scanf("%d",&z)==1) scanf("%*[^0-9] %d",&z);
    printf("%d %d %d\n",x,y,z);
   return 0;
}

Input & Output

fehwih 2738     @$!(#)12[3]
2738 12 3

Reference from: http://www.cplusplus.com/reference/cstdio/scanf/

Upvotes: -2

Igor
Igor

Reputation: 1855

A little more complicated solution, but prevents overflow of array and works for any kind of input. get_numbers_from_input function takes array where read numbers will be put and maximum count of numbers in array and returns count of numbers read from standard input. function reads characters from standard input until enter is pressed.

#include <stdio.h>    
//return number readed from standard input
//numbers are populated into numbers array
int get_numbers_from_input(int numbers[], int maxNumbers) {
    int count = -1;
    char c = 0;
    char digitFound = 0;
    while ((c = getc(stdin)) != '\n') {
        if (c >= '0' && c <= '9') {
            if (!digitFound) {
                if (count == maxNumbers) {
                    break; //prevent overflow!
                }
                numbers[++count] = (c - '0');
                digitFound = 1;
            }
            else {
                numbers[count] = numbers[count] * 10 + (c - '0');
            }
        }
        else if (digitFound) {
            digitFound = 0;
        }
    }

    return count + 1; //because count starts from -1
}

int main(int argc, char* argv[])
{
    int numbers[100]; //max 100 numbers! 

    int numbersCount = get_numbers_from_input(numbers, 100);
    //output all numbers from input
    for (int c = 0; c < numbersCount; ++c) {
        printf("%d ", numbers[c]);
    }
    return 0;
}

Upvotes: 0

Bharat
Bharat

Reputation: 3000

You could read a string, use a scan set to filter it and convert it to an integer.

See scanf: http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char num1[256], num2[256], num3[256]; 

    scanf("%s %s %s", num1, num2, num3);
    sscanf(num1, num2, num3, "%[0-9]d %[0-9]d %[0-9]d", num1, num2, num3);

    int n1 = atoi(num1), n2 = atoi(num2), n3 = atoi(num3); // convert the strings to int

    printf("\n%d %d %d\n", n1, n2, n3);

    return 0;
}

Sample Input & Output:

2332jbjjjh 7ssd 100

2332 7 100

Upvotes: 4

Related Questions