2

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;
}
10
  • Try fflush(stdin) before the next scanf statement Commented Jan 28, 2014 at 9:11
  • 9
    @SakthiKumar: No, never use fflush(stdin) - it results in UB on many platforms. Commented Jan 28, 2014 at 9:14
  • 1
    @SakthiKumar, thanks for your input. Paul R, what would you recommmend? Commented Jan 28, 2014 at 9:15
  • 1
    You can use fpurge if your platform has it, or for maximum portability just read characters and discard until you hit a newline. Commented Jan 28, 2014 at 9:17
  • 3
    Oops! fflush is only for output streams. Sorry for the comment. Commented Jan 28, 2014 at 9:20

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

@MIIJ If you find an answer helpful (and correct), then thank them by up-voting the answer. Click on the arrow pointing upwards on the left side of the post. So in this case maybe up-vote both answers and accept the one you liked best (which you already did).
@Lundin, thanks for the tip, I tried before but it seems I don't have enough "reputation" to do so. Will do so as soon as I can.
@Alter Mann, Is the following correct? [if ((p = strchr(buf, '\n')) != NULL) //if the buf holds a '\n' value (return key)]...[ *p = '\0'; //the p char variable is set to null-terminator, signifying the end of a string]...[ x = (int)strtol(buf, &p, 10); //x is the parsing, at base 10, from the chars in the buf, until the value of p, to int.]...[ if (p == buf || *p != '\0') // if p hasn't been parsed, that it is exactly equal to the buffer values //2) I don't understand the 2nd entity(when it is equal to only 'return')?] ... when does it check for letters?
@MIIJ, you can translate this as: if user types nothing or strtol fails to read a number
@Alter Mann, thanks for your answer, I had failed to see that the "base 10" in the strtol was the filter for letters... 'a' does not have an int equivalent. Learning, learning.
1

The code works fine for integer also. The only mistake I found is while printing the sorry message, you are printing just i, it should be i+1.

    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+1);
      }
    }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.