gpt4 book ai didi

c - 在 C 中执行 while 循环

转载 作者:行者123 更新时间:2023-12-04 12:32:41 25 4
gpt4 key购买 nike

问题 我输入 y 作为选项,它打印了两次文本,然后再次提示我,而它应该只打印一次。

正确的输出(我应该得到但没有得到的):

Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut)

Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut)

Do you order FISH (Y/N)? n

Do you order CHIPS (Y/N)? n

Do you order DRINKS (Y/N)? y
Drinks choice (S- Softdrink, C- Coffee, T- Tea)

Do you order DRINKS (Y/N)? n

WRONG OUTPUT(我得到的输出)

Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut)
Do you order FISH (Y/N)? Fish choice (K- Haddock, T- Halibut)
Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut)
Do you order FISH (Y/N)? Fish choice (K- Haddock, T- Halibut)
Do you order FISH (Y/N)? n
Do you order CHIPS (Y/N)? Chips choice (C- Cut, R- Ring)
Do you order CHIPS (Y/N)? n
Do you order DRINKS (Y/N)? Drinks choice (S- Softdrink, C- Coffee, T- Tea):
Do you order DRINKS (Y/N)? n

RAW SOURCE(如果您想自己编译并检查最新情况):http://pastebin.com/raw.php?i=mZ1jVrF0

来源

#include <stdio.h>
#include <string.h>

int main() {

char fishYesNo, chipsYesNo, drinksYesNo;
char *typeOfFood;

do {
typeOfFood = "fish";
printf("Do you order FISH (Y/N)? ");
scanf("%c", &fishYesNo);
if (fishYesNo != 'n') {
printf("Fish choice (K- Haddock, T- Halibut) \n");

}
else if (fishYesNo == 'n') {
typeOfFood = "chips";
}

} while ((strcmp(typeOfFood, "fish")) == 0);

do {
typeOfFood = "chips";
printf("Do you order CHIPS (Y/N)? ");
scanf("%c", &chipsYesNo);
if (chipsYesNo != 'n') {
printf("Chips choice (C- Cut, R- Ring) \n");
}
else if (chipsYesNo == 'n') {
typeOfFood = "drinks";
}

} while ((strcmp(typeOfFood, "chips")) == 0);

do {
typeOfFood = "drinks";
printf("Do you order DRINKS (Y/N)? ");
scanf("%c", &drinksYesNo);
if (drinksYesNo != 'n') {
printf("Drinks choice (S- Softdrink, C- Coffee, T- Tea):\n");
}
else if (drinksYesNo == 'n') {
typeOfFood = "fish";
}

} while ((strcmp(typeOfFood, "drinks")) == 0);

}

最佳答案

这是 C 中一个众所周知的概念错误。 comp.lang.c FAQ (我建议你阅读)有很多关于这个问题和其他问题的信息。

代码中发生的事情的解释(comp.lang.c FAQ list · Question 12.18b):

You wanted scanf %c to read a single character, and it tried to, but when you tried to type that single character at it, before the rest of the input system would accept it, you had to hit the RETURN key, too. scanf read only the one character, but that extra newline was still sitting in an input buffer somewhere, and it's that extra newline (seemingly representing a phantom blank line) which was received by your later input call.

如何处理它,来自对 scanf 问题的回顾(comp.lang.c FAQ list · Question 12.20):

It's nearly impossible to deal gracefully with all of these potential problems when using scanf; it's far easier to read entire lines (with fgets or the like), then interpret them, either using sscanf or some other techniques.

关于c - 在 C 中执行 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9917298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com