0
char character, newLine = 'x';

while (newLine != '\n') {
    scanf("%c%c", &character, &newLine);

    if (newLine != '\n')
        printf("ERROR: enter 1 character\n");
}

For example, if I enter "KITTEN", I want it to analyze only "KI", output 1 error message and wait for the next input. Instead, it analyzes "KI", "TT", "EN" and as the result outputs 3 error messages.

  • Basically you want to consume the whole line of input. Use `fgets` to read the whole line then examine the first two characters. That is the safest way. Else can keep reading one character at a time (ignoring the value that is read) until a newline character is encountered. – kaylum Nov 13 '21 at 21:07

3 Answers3

1

You cannot do that with only scanf(). You need to somehow read in the remaining characters on the line before you can read the next line. Something like

scanf("%c%c", &character, &newLine);
if (newLine != '\n') {
    fprintf(stderr, "ERROR: enter 1 character\n");
    while (getchar() != '\n')
        ;
}

This code will do what you are asking for, but isn't great. It doesn't handle the case where the user enters just a '\n' character (the scanf() routine won't return until they enter a second character to read).

If you want to do line-by-line processing, you would be better-served to use fgets() to read in a line, and the sscanf() to parse it.

One Guy Hacking
  • 1,176
  • 11
  • 16
1

Two different answers.

  1. There's no good way to do this with scanf. One of scanf's giant limitations is that it never, ever reads anything it doesn't like or that you didn't tell it to. So you can make it explicitly read the two characters you asked for with %c%c, but there's no almost no way to make it "discard the rest of the line and wait for the next input". And because scanf is so limited, any time you have a question like "How do I do this specific, slightly fancy, error-detecting thing with scanf?", the answer is virtually always, "Don't use scanf. Read a whole line, with fgets or the like, then process it."

  2. I said "there's no almost no way to make it 'discard the rest of the line'", but I lied. There is a way: %[…]. So you might (might!) be able to do something along the lines of:

    int n = scanf("%c%c%*[^\n]", &character, &dummy);
    if(n != 1)
    printf("ERROR: enter 1 character\n");

Now, with that said, my opinion is that answer #2 is horrible, and I do not recommend that you use it. scanf has precisely one virtue, and that is that it is superficially easy to use for basic input in the very early programs that beginners can write when they're first learning C. But notations like ^%*[^\n] are the polar opposite of "superficially easy". (Also the example I presented in answer #2 won't quite work — see the comments, and chux's answer, for details.) So I urge you to use answer #1. See more information at What can I use for input conversion instead of scanf?.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
0

to make scanf ignore the rest of the input other than the first 2 symbols?

Use "%1[^\n]" twice to read a one non-'\n' character as a string.

When needed, use "%*[^\n]" to consume N non-'\n' characters.

// Return 1 on success
// Return EOF on end-of-file
int read_first2(char buf[3], const char *error_msg) {
  while (1) {
    char eol[2];
    // scan_result will be 3, 2, 1, 0 or EOF
    int scan_result = scanf("%1[^\n]%1[^\n]%1[\n]"`, &buf[0], &buf[1], eol);
    if (scan_result == 3) {
      // Code only gets here if well formed. 
      return 1;
    }
    if (scan_result == EOF) {
      return EOF;
    }
    // Consume rest of the line
    scanf("%*[^\n]"`);  // Consume rest of line except \n
    scanf("%*1[\n]"`);  // Consume \n
    puts(error_msg);
  }
}

I rather use fgets().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256