0

Is it possible to ignore scanf() in a while loop if a certain character was scanned?

For example:

int i = 0;
char b = 32;

while (scanf("%c", &b) == 1) {
    if (b == '\n') {
        printf("r");
    } 
    i++;  
}

output after pressing enter: "r". output after pressing whitespace+enter: "r". But here I expect no output! I want the output only when an enter is pressed and nothing before!

modar
  • 37
  • 4
  • Welcome to Stack Overflow. An `if` statement is never "ignored". What happens is that the condition either is true when you expected it to be false, or vice versa - possibly because the relevant surrounding code (in this case, most likely the `scanf` operation itself) doesn't do what you expect. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and then give a clear explanation, for a *specific* input, of *exactly* what you think should happen *exactly* what happens instead, and why you are confused even after trying to follow the code logic step by step. – Karl Knechtel Sep 27 '21 at 19:40
  • If the problem is that you understand what's going wrong but can't figure out how to write code that implements the desired logic, you'll still need to explain in detail what should happen for a specific input, and state the logic clearly. Also, try taking out a real pencil and piece of paper and drawing a flowchart. It really does work. – Karl Knechtel Sep 27 '21 at 19:41
  • thanks for answering! I edited the question i hope its now clear what i mean. – modar Sep 27 '21 at 19:53
  • 1
    If you're reading interactive input then indeed, you will need to accommodate occasional newlines generated by enter keypresses. You can certainly ignore these. However, if the input is expected also to contain newlines that you wish to process differently then either you need some way to distinguish between the two kinds, or else your input format is not suitable for interactive use. – John Bollinger Sep 27 '21 at 19:54
  • i really need to ignore everything but an enter, anything else should be ignored! – modar Sep 27 '21 at 19:58
  • See https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c – spicy.dll Sep 27 '21 at 20:00
  • Does this answer your question? [How to clear input buffer in C?](https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c) – spicy.dll Sep 27 '21 at 20:01
  • In your question, you wrote: `"output after pressing whitespace+enter: "r". But here I expect no output!"` -- The first iteration of the loop will not output anything, because it will read the non-newline whitespace character. Only in the second iteration of the loop will it read the newline character, causing it to print `"r"`. What exactly is the intended behavior? Do you not want the loop to iterate a second time? In that case, remove the loop. – Andreas Wenzel Sep 27 '21 at 20:03
  • @AndreasWenzel exactly thats the problem. I want it to loop again for another scan. But the next loop should not print anything until only Enter is pressed! – modar Sep 27 '21 at 20:11

1 Answers1

2

You can use a flag to indicate whether there have been other characters before the newline. Something like:

char b = 32;
int flag = 1;

while (scanf("%c", &b) == 1) 
{
    if (b == 'q') break;  // End program when q is pressed

    if (b == '\n') 
    {
        // Only print if the flag is still 1
        if (flag == 1)
        {
            printf("r");
        }

        // Set the flag to 1 as a newline starts
        flag = 1;
    }
    else
    {
        // Other characters found, set flag to 0
        flag = 0;
    }
}

For input like

\n   // newline will print an r
 \n  // space + newline will not print anything
a\n  // a + newline will not print anything

In general this code will only print when input is a newline without any preceeding characters.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63