scanf check in while loop to restrict integer input

MIIJ

I am writing a code asking the user to input 10 integers, which are then fed to him backwards. I would like to create a "scanf check" to restrict character input. The while loop works insofar that it doesn't accept char, but it skips a integer input.

int main()
{

    int i = 0, number[10] = {0};
    char buf[128] = {0};

    for (i = 0; i < 10; i++)
    {
      printf("Please input number %d : ", i+1);

         while(scanf("%d", &number[i]) != 1)
      {
         scanf("%s", &buf);
         printf("Sorry, [%s] is not a number. Please input number %d : ", &buf, i);
      }
    }

    for (i = 0; i < 10; i++)
    {
    printf("\n Number %d is %d", (10-i), number[9-i]);
    }

    return EXIT_SUCCESS;
}
David Ranieri

As pointed out by H2CO3, don't use scanf, an alternative is fgets and strtol:

int i, number[10] = {0};
char buf[128], *p;

for (i = 0; i < 10; i++) {
    printf("Please input number %d : ", i+1);
    while (1) {
        fgets(buf, sizeof(buf), stdin);
        if ((p = strchr(buf, '\n')) != NULL) {
            *p = '\0';
        }
        number[i] = (int)strtol(buf, &p, 10);
        if (p == buf || *p != '\0')  {
            printf("Sorry, [%s] is not a number. Please input number %d : ", buf, i + 1);
        } else {
            break;
        }  
    }
}
for (i = 0; i < 10; i++) {
    printf("\n Number %d is %d", (10-i), number[9-i]);
}
return EXIT_SUCCESS;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

proper use of scanf in a while loop to validate input

From Dev

While loop not waiting for input with scanf("%d")

From Dev

Checking input types with scanf() in a while loop

From Dev

Check loop if input are all integer

From Dev

Check if input is a number in a while loop

From Dev

Check if input is a number in a while loop

From Dev

scanf skipped after reading in integer in C, in while loop

From Dev

while loop to check for user input not in for loop

From Dev

C Programming: Scanf in while loop only reads input once and then terminates

From Dev

Using scanf with a while loop to input two separate strings

From Dev

Check the input of the scanf

From Dev

While loop printing and scanf

From Dev

While loop to check for valid user input?

From Dev

How to check if integer is positive or negative in a WHILE loop in Java?

From Dev

Java - cumulative sum of integer input doesnt work in while loop

From Dev

While loop using a counter utilizing user's input of an integer

From Dev

Reading a finite input from user through while loop into an integer variable

From Dev

Is it possible to check if the input is an integer without hasNextLine and exit loop if it's 0?

From Dev

using scanf to check input parameters

From Dev

using scanf to check input parameters

From Dev

How to check scanf input that is not identifiable by %

From Dev

How to restrict a while loop in java?

From Dev

Escape scanf within a while loop

From Dev

while loop does not execute scanf()

From Dev

Check if input is integer

From Dev

Check for input being an integer

From Dev

Scanf to check reading of integer doesn't work

From Java

scanf error on while loop (and for loop), scanning forever

From Dev

cin >> integer and while loop

Related Related

  1. 1

    proper use of scanf in a while loop to validate input

  2. 2

    While loop not waiting for input with scanf("%d")

  3. 3

    Checking input types with scanf() in a while loop

  4. 4

    Check loop if input are all integer

  5. 5

    Check if input is a number in a while loop

  6. 6

    Check if input is a number in a while loop

  7. 7

    scanf skipped after reading in integer in C, in while loop

  8. 8

    while loop to check for user input not in for loop

  9. 9

    C Programming: Scanf in while loop only reads input once and then terminates

  10. 10

    Using scanf with a while loop to input two separate strings

  11. 11

    Check the input of the scanf

  12. 12

    While loop printing and scanf

  13. 13

    While loop to check for valid user input?

  14. 14

    How to check if integer is positive or negative in a WHILE loop in Java?

  15. 15

    Java - cumulative sum of integer input doesnt work in while loop

  16. 16

    While loop using a counter utilizing user's input of an integer

  17. 17

    Reading a finite input from user through while loop into an integer variable

  18. 18

    Is it possible to check if the input is an integer without hasNextLine and exit loop if it's 0?

  19. 19

    using scanf to check input parameters

  20. 20

    using scanf to check input parameters

  21. 21

    How to check scanf input that is not identifiable by %

  22. 22

    How to restrict a while loop in java?

  23. 23

    Escape scanf within a while loop

  24. 24

    while loop does not execute scanf()

  25. 25

    Check if input is integer

  26. 26

    Check for input being an integer

  27. 27

    Scanf to check reading of integer doesn't work

  28. 28

    scanf error on while loop (and for loop), scanning forever

  29. 29

    cin >> integer and while loop

HotTag

Archive