我正在编写一个有趣的程序(不是为了学校),并且我很难弄清楚为什么scanf
函数在我的循环的每次迭代中都没有执行 - 我已经同时玩弄了两个'循环和'而#39;循环。
我知道,根据我写scanf
函数的方式(即scanf("%s", &variablename);
VS scanf("%99[^\n]s", &variablename);
)会有所不同,但我已经尝试了所有内容,而且我绝望了! / p>
当我对来自printf
的输入进行scanf
检查时,在每次迭代时,每次迭代只会输入一个字符串,所以如果我在第一次输入中输入两个单词,则需要要处理的两次迭代 - 每个一个单词。以下是我所描述的代码段:
int main(void){
int tries = 0;
int score = 0;
char question[100];
char useranswer[100];
const char *phrase = {"our favorite saying\0"};
printf("\nQuestion #3 (10 points): What is our secret saying?\n");
sleep(1);
tries = 1;
while (tries<=3){
printf("YOUR ANSWER:");
scanf("%s[^\n]", useranswer);
if(strncmp(useranswer, phrase, 15) != 0){
printf ("Nope, try again!\n");
printf("You have used %d out of 3 tries!\n", tries);
if (tries == 2){
printf("Here's your final hint:xxx...\n");
}
if (tries == 3){
printf("You didn't get it. The answer is: our favorite saying!\n");
}
tries++;
}
if (strncmp(useranswer, phrase, 15) == 0){
printf("Damn, you're good. Well done.\n");
score += 10;
break;
}
}
此代码的输出为:
Question #3 (10 points): What is our secret saying?
YOUR ANSWER:our favorite saying
Nope, try again!
You have used 1 out of 3 tries!
YOUR ANSWER:Nope, try again!
You have used 2 out of 3 tries!
Here's your final hint:xxx...
YOUR ANSWER:Nope, try again!
You have used 3 out of 3 tries!
You didn't get it. The answer is: our favorite saying!
(它只允许我输入一次,然后输入&#34;我们最喜欢的一句&#34;。)
答案 0 :(得分:3)
在评论中,您可以找到scanf
中的格式说明符不起作用的原因。
另一种方法是使用fgets
代替,也许在辅助函数中,它可以处理读取用户输入时可能出现的一些极端情况:
#include <ctype.h>
char *read_line( char *buf, size_t n, FILE *pfin )
{
ssize_t length = 0;
int ch;
if ( !buf || n == 0 )
return NULL;
/* Consume trailing control characters, like '\0','\n', '\r', '\f'...
but also '\t'. Note that ' ' is not skipped. */
while ( (ch = fgetc(pfin)) != EOF && iscntrl(ch) ) { }
if ( ch == EOF )
return NULL;
/* At least one char is printable */
*buf = ch;
++length;
/* Read from file till a newline or up to n-2 chars. The remaining chars
are left in the stream buffer. Return NULL if no char is read. */
if ( fgets(buf + 1, n - 1, pfin) )
{
/* Trim the string to the first control character */
while ( !iscntrl(buf[length]) )
{
++length;
}
buf[length] = '\0';
}
return buf;
}
我也改变了以下逻辑。 OP多次使用strncmp(useranswer, phrase, 15)
,但该幻数15
低于phrase
的大小,因此最终仅比较子字符串。
while ( tries <= 3 ) {
printf("YOUR ANSWER:");
if ( !read_line(useranswer, sizeof useranswer, stdin) ) {
printf("Error: Unexpected end of input.\n");
exit(EXIT_FAILURE);
}
if( strcmp(useranswer, phrase) == 0 ) {
printf("Damn, you're good. Well done.\n");
score += 10;
break;
} else {
printf ("Nope, try again!\n");
printf("You have used %d out of 3 tries!\n", tries);
if (tries == 2) {
printf("Here's your final hint:xxx...\n");
}
if (tries == 3) {
printf("You didn't get it. The answer is: our favorite saying!\n");
}
tries++;
}
}
<小时/> 作为最后一点,我发现OP
phrase
的声明有点奇怪(可能是一个错字):
const char *phrase = {"our favorite saying\0"};
// string literals are already ^^ null terminated...
虽然我们可以使用简单的数组声明,例如:
const char phrase[] = "our favorite saying";
还要考虑在这两种不同情况下sizeof phrase
返回的值。
<小时/> 感谢 @chux 提供了所有有价值的提示和提供的有趣链接:
strcmp
代替strncmp
。