admin管理员组

文章数量:1122832

I am trying to use a negated scanset technique with scanf (I am not allowed to use anything else so no getchar()).

But I have a small problem with it.

When I use scanf(“%*[ \n]%*c”). If I enter an input like 4 (space!) 4 It won’t clean the buffer for some reason? The 2nd 4 is still there, it prints my error message twice because of that.

I tried implementing %*[^ ] but it ruins the code in cases like 45 where there are no spaces and thus not continuing to run the code, just stuck on the scanf.

By stuck, I mean not getting the error messages that I want to print; instead I need to keep entering numbers/strings until it decides to pop.

I really don't know what to do. I didn’t implement my code here because I deleted it all.

Just a simple scanf for integers While(1) If the result of the scanf isn’t 2 printing an error message Using the negated scanf Scanf-ing into the integer

If it’s 2 break out.

ChatGPT, YouTube, Reddit, Google, even here

Literally nothing … from what I read what I did should work but it doesn’t, and I really don’t know why.

example code:

scanFailed = scanf(" numer:%d,number:%d");
while (scanFailed != 2) {
    scanf("%*[ \n]%*c");
    scanf(" %c", &char1);
    scanf("%*[ ]");
    scanf(" %c", &char2);
    scanf("%*[ ]%*c");
    printf("Enter the number : \n");
    scanFailed = scanf(" numer:%d,number:%d");
}

I am trying to use a negated scanset technique with scanf (I am not allowed to use anything else so no getchar()).

But I have a small problem with it.

When I use scanf(“%*[ \n]%*c”). If I enter an input like 4 (space!) 4 It won’t clean the buffer for some reason? The 2nd 4 is still there, it prints my error message twice because of that.

I tried implementing %*[^ ] but it ruins the code in cases like 45 where there are no spaces and thus not continuing to run the code, just stuck on the scanf.

By stuck, I mean not getting the error messages that I want to print; instead I need to keep entering numbers/strings until it decides to pop.

I really don't know what to do. I didn’t implement my code here because I deleted it all.

Just a simple scanf for integers While(1) If the result of the scanf isn’t 2 printing an error message Using the negated scanf Scanf-ing into the integer

If it’s 2 break out.

ChatGPT, YouTube, Reddit, Google, even here

Literally nothing … from what I read what I did should work but it doesn’t, and I really don’t know why.

example code:

scanFailed = scanf(" numer:%d,number:%d");
while (scanFailed != 2) {
    scanf("%*[ \n]%*c");
    scanf(" %c", &char1);
    scanf("%*[ ]");
    scanf(" %c", &char2);
    scanf("%*[ ]%*c");
    printf("Enter the number : \n");
    scanFailed = scanf(" numer:%d,number:%d");
}
Share Improve this question edited Nov 23, 2024 at 10:18 chqrlie 144k12 gold badges130 silver badges207 bronze badges asked Nov 23, 2024 at 0:55 oriori 212 bronze badges 6
  • 1 You don't show a negated scan set in the first example. You should also specify what you're trying to do more clearly. Also, show what's gone before. Remember that when you suppress a conversion specification, it doesn't get counted. – Jonathan Leffler Commented Nov 23, 2024 at 1:20
  • Please just say: what actual input you want your pattern to match. I think you're trying to match pairs of integers separated by whitespace? Also sorting what you tried that didn't work (and what happened, and what you wanted to happen instead) is useful. – Useless Commented Nov 23, 2024 at 1:23
  • @ori Try char buf[100]; scanf(" %99[^\n]", buf); to read the line of input into a string. Then parse the string into a number or whatever. – chux Commented Nov 23, 2024 at 2:12
  • Sadly I can’t use an array :(. Regarding my input, I want to scan something specific, if it doesn’t go like “number:<int>” I want to ignore it and get rid of it from the buffer @Useless – ori Commented Nov 23, 2024 at 9:19
  • 1 Is the user really meant to type: numer:13,number:23 (with their chosen numeric values)? It seems a little excessive — but that's what your initial scanf() requires, complete with differential spelling on numer vs number. – Jonathan Leffler Commented Nov 23, 2024 at 18:53
 |  Show 1 more comment

1 Answer 1

Reset to default 1

The basic problem is that scanf scansets (whether negated or not) match 1 or more characters, and will fail to match when there is no such character.

Thus the pattern "%*[ \n]%*c" will match one or more spaces or newlines, followed by any one character. If the next character in the input is NOT a space or newline, it will fail and not match anything.

So if you want to ignore something that may or may not be there, a scanset will only work if it is in a scanf by itself. So if you want to read 2 non-space characters, with optional spaces before and between using scansets, you need something like:

scanf("%*[ ]");
scanf("%c", &char1);
scanf("%*[ ]");
scanf("%c", &char2);

four separate calls to scanf -- if there are no spaces, then those scanf calls will fail, but consume nothing.

The only zero or more match possible in scanf is when using a space -- this will match zero or more spaces, tabs, or newlines. So if you want two non-whitespace characters while skipping whitespace, you can use

scanf(" %c %c", &char1, &char);

But this only works if what you want to skip is any whitespace (any space, tab, or newline) and don't care about the difference between different kinds of whitespace.

In general, you should also check the return value of scanf -- you might get an unexpected end-of-file otherwise.

本文标签: scanfNegated scanset technique in CStack Overflow