【发布时间】:2015-08-20 23:42:56
【问题描述】:
以下代码有什么问题?为什么我输入c后,程序会输出两次“请选择命令:”?还有为什么我先输入i,再输入e,程序不会输出“you choose e”
#include <stdio.h>
void interface(){
char command;
printf("please choose the command: \n");
scanf("%c",&command);
if (command == 'c'){
printf("you choose c\n");
}
else if (command == 'i'){
printf("you choose i, what is next?: \n");
scanf("%c",&command);
if (command == 'e'){
printf("you choose e\n");
}
}
else if (command == 'p'){
printf("you choose p, what is next?: \n");
scanf("%c",&command);
if (command == 'a'){
printf("you choose a\n");
}
}
}
int main(int argc, char const *argv[])
{
while(1){
interface();
}
return 0;
}
【问题讨论】:
标签: c while-loop scanf