问题描述
我是学习C语言的新手。 我已经从C ++转向使用了while并执行while循环
以下是我的代码的摘录。这在C ++中工作正常,但在执行while循环后的C中,它首先执行scanf然后打印菜单,但我无法找到它。请帮助
#include <stdio.h>
int main() {
int ch;
char ans='y';
while(ans=='y'){
printf("\n1. Insert / Create linked list ");
printf("\n2. Insert at specific position ");
printf("\n3. display ");
printf("\n4. Search element");
printf("\n5. Delete element");
printf("\n6. Exit");
printf("\nEnter choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
}
printf("\nDo you want to continue ?\n");
scanf(" %c ",&ans);
printf("\n");
};
return 0;
}
解决方法
...prevState.categories
-> scanf(" %c ",&ans);
删除尾随空格。
无需在scanf(" %c",&ans);
之后查找空格。