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
|
Show 1 more comment
1 Answer
Reset to default 1The 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
版权声明:本文标题:scanf - Negated scanset technique in C - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300196a1930730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:12numer:13,number:23
(with their chosen numeric values)? It seems a little excessive — but that's what your initialscanf()
requires, complete with differential spelling onnumer
vsnumber
. – Jonathan Leffler Commented Nov 23, 2024 at 18:53