1

I cannot get isdigit to work. Here is some of my code.

void input(int *array, int size) {
    int x;
    printf("Give me ten numbers. \n");
    for (int i = 0; i < size; i++) {
        do {
            printf("Array[%d]: ", i);
            scanf("%d", &x);
            scanf("%*[^\n]");
        } while (!isdigit(x));
        array[i] = x;
    }
}

The aim of this program is to read an integer from the keyboard with scanf (which is no problem). If the input is not a number, it should repeat the while loop, until the user gives a number. With isdigit, you should be able to identify if a character is an integer or not. It returns 1 if integer, 0 if not. At least, that should be the case. In this code it always returns 0.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 2
    What does "not work" mean, to you? Please describe the outcome, and the expected outcome. Also, remember that I/O can fai: you should check the return value of `scanf()`. – unwind Dec 14 '16 at 12:04
  • What is the aim of this code? – RoadRunner Dec 14 '16 at 12:09
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Dec 14 '16 at 12:20
  • `scanf("%*[^\n]"); // fflush does not work on Macs. This is an alternative.` does not scan in a `'\n'`. So any will only be read with `scanf("%c", &x);`. Recommend using `fgets(buffer, ...)` for user input and then scan the buffer for the desired text – chux - Reinstate Monica Dec 14 '16 at 14:18

2 Answers2

1

I cannot understand what are you trying to do. First if you need the integer number of a charachter you should leave the value '0' so

c-'0'.

Then rather than use scanf, if you should just get a charachter much better getchar() to flush just

 while(getchar()!='\n')
              ;

Than if you do not want use the proper scanf("%d",&int_variable); you have to build a function to rebuild the number, it means take the charachter, leave '0' and multiply by ten at each following charachter.

jurhas
  • 613
  • 1
  • 4
  • 12
0

Sweet problem!! isdigit take argument a single character(ASCII Value) and determine whether the character is digit or not . [so, it will work for signal digit].

first, as isdigit take argument a single character, if you pass the digit as argument then it will say "NO IT IS NOT NUMBER". You should pass ASCII value of single digit number as argument of isdigit.

I mean, '1' != 1

'1' is equal to 1+'0' which is ASCII value of character '1'.

Solution 1: [It will work for single digit]

void input(int *array, int size) {
    printf("Give me ten numbers. \n");
    for (int i = 0; i < size; i++) {
        int x;
        do {
            x = 'a'; //bcz if user enter a character, scanf will not able to take the value, so, x will hold the previous value
            printf("Array[%d]: ", i);
            scanf("%d", &x);
            scanf("%*[^\n]"); // fflush does not work on Macs. This is an alternative. [I don't know about the line, I copied from you.]
        } while (!isdigit(x+'0')); //converting the value to character

        array[i] = x;
    }
}

Solution 2 : (For pro) :p single line code :)

void input(int *array, int size) {
    printf("Give me ten numbers. \n");
    for (int i = 0; i < size; i++) {
        while( printf("array[%d] = ",i) && !scanf("%d",&array[i]) && !scanf("%*[^\n]")); // it will give same result.
    }
}
Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37