0

I have scoured the internet and my textbooks and lectures for a way to fix this issue and nothing appears to be working.

I am using scanf to get an integer input, and need to validate the correct input.

However, when I put say, two characters or two numbers with a space inbetween into the scanf, it runs through twice instead of once as scanf hangs on to the unused data.

I cannot for the life of me find out how to either clear the scanf data, or to get it to read all of the input together as one incorrect input. I know how to clear the \n, just not the trailing characters that get left in the input.

int scan;
bool validInput = false;
int validScan;
int count = 0;
do 
{
    puts("Enter pin : ");
    validScan = scanf("%d", &scan);
    ((getchar()) != '\n');

    if(validScan !=1)
    {
        count++;
        puts("Incorrect pin entered.");
    }
    else if(scan != PIN)
    {
        count++;
        puts("Incorrect pin entered.");
    }
    else
    {
        validInput = true;
        count = 3;
    }
} while (count < 3);
return validInput;
  • Post your code? – MFerguson Nov 17 '21 at 01:24
  • 2
    If your `scanf()` call fails, it is usually sensible to read up to and including the next newline character: `int c; while ((c = getchar()) != EOF && c != '\n') ;` (where the semicolon after the loop would normally be on a line on its own to indicate an empty loop body). You might create an `inline` function to do that job, or a real function. Reading lines with `fgets()` and parsing with `sscanf()` is also a very good technique — not least because you can report on the input that causes trouble more sensibly (it's saved in the input buffer used by `fgets()` and `sscanf()`). – Jonathan Leffler Nov 17 '21 at 01:58
  • See also [Using `fflush(stdin)`?](https://stackoverflow.com/q/2979209/15168) and read with care and use with extreme caution (preferably, do not use). Also see [How to read/parse input in C? The FAQ](https://stackoverflow.com/q/35178520/15168). – Jonathan Leffler Nov 17 '21 at 02:00
  • PleaseHelpMe222, What should happen if the _line_ of user input is only `"\n"`? – chux - Reinstate Monica Nov 17 '21 at 03:02

2 Answers2

1

However, when I put say, two characters or two numbers with a space inbetween into the scanf, it runs through twice instead of once as scanf hangs on to the unused data.

You need to decouple the interpretation of the user input from reading the user input.

So instead of using scanf that does both at the same time:

int num1, num2;
bool success = (scanf("%d %d", &num1, &num2) == 2);

Use fgets to read it and sscanf to interpret it:

char buf[1024];
bool success = false;
if(fgets(buf, 1024, stdin) != NULL)
    success = (sscanf(buf, "%d %d", &num1, &num2) == 2);

See also scanf vs fgets and disadvantages of scanf.

-1

you could use fflush(stdin); before a scanf to clean the input.

  • 1
    I suggest that you read [this question](https://stackoverflow.com/q/2979209/12149471) to understand why this is, in general, bad advice. – Andreas Wenzel Nov 17 '21 at 03:04
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 17 '21 at 05:29