0

As a part of an assignment I've got, I need to get from a user a digit only using the <stdio.h> library.

do {
  scanf("%d",&num);
} while (num>9 || num<0);

But the program will get from the user also chars and other things.

Any advice?

Mat
  • 202,337
  • 40
  • 393
  • 406
Tal Alfi
  • 35
  • 6
  • Too unclear, please elaborate. – Jabberwocky Apr 06 '18 at 12:18
  • 1
    Read a line, convert the string in line to `int`? There are [functions](http://en.cppreference.com/w/c/string/byte/strtol) which can convert a number in a string to an integer, *with* validation of the string. – Some programmer dude Apr 06 '18 at 12:19
  • 1
    Do check for return value by scanf. If successful, the total number of characters written is returned by `scanf()`, otherwise a negative number is returned. –  Apr 06 '18 at 12:19
  • @Someprogrammerdude those functions are not a part of stdio.h. – Pranav Totla Apr 06 '18 at 12:27
  • @PranavTotla No, `strtol` is not declared in ``, it's declared in `` which is still a *standard* C header file. All C compilers should have it and the function. – Some programmer dude Apr 06 '18 at 12:41
  • @Dominique How could both `num>9` and `num<0` be true at the same time? If one is true then the other must be false, and using AND would make the whole combined condition false and break out of the loop, which is clearly wrong. – Some programmer dude Apr 06 '18 at 12:48

2 Answers2

0

One way of solving this is getting the ASCII value of the scanned "digit" and making sure it lies between ASCII values 0X30 and 0X39 (or 48 and 57).

Seeing your logic, you seem to be interested in numbers greater than 9 (multiple digit numbers) and numbers less than 0 (signed numbers/integers).

I suggest instead of using scanf better use something like getchar which will return one character at a time.

A simple way to scan multiple digit numbers using getchar can be:

int getNumber()
{
    int sign = 1;
    int i = 0;
    int ch;
    ch = getchar();
    if((ch == '-') || (ch == '+')) //check sign
    {
        if(ch == '-') sign = -1;
    }
    else  
    {
        if (ch > '9' || ch < '0')
            return NULL; // Your requirement - it's a non number
        i *= 10;
        i += ch - '0';
    }
    while ((ch = getchar()) != '\n') //get remaining chars
    {
        if (ch > '9' || ch < '0')
            return NULL; // Your requirement - it's a non number
        i *= 10;
        i += ch - '0';
    }
    i *= sign; //change sign
    return i;
}

You can call this function in place of scanf.

Pranav Totla
  • 2,182
  • 2
  • 20
  • 28
0

You can use getchar() to read a single character from stdin and then compare it to the ASCII values for 0 and 9 (your upper and lower bounds for a single character). The digit as an integer is equal to the character code minus the character code for 0.

#include <stdio.h>

int main(void) {
    char c;

    while((c = getchar()) >= '0' && c <= '9')
        printf("Digit is: %d\n", c - '0');
    return 0;
}
yoker
  • 1