我这个循环有啥问题吗,为啥没有按顺序再执行m = getchar(),却执行了default
#include <stdio.h>
int main(){
double price, tra_pac_cost, pounda, poundb, poundc, sumpound,suma, sumb, sumc;/*总菜价,运输与包装费用,输入洋蓟磅数,输入甜菜磅数,输入胡萝卜磅数,总磅数,总计洋蓟磅数,总计甜菜磅数,总计胡萝卜磅数*/
char m;//选择菜品
int n = 0;//输入失败计数
printf("*********************************************************************");
printf("\nEnter the number corresponding to the your corresponding condition: ");
printf("\na) 洋蓟:2.05美元/磅\t\tb) 甜菜:1.15美元/磅");
printf("\nc) 胡萝卜:1.09美元/磅\tq) quit");
printf("\nn)进入下一步");
printf("\n*********************************************************************\n");
printf("Enter a number to select:\n");
m = getchar();
while(1){
switch(m)
{
case 'a':printf("输入洋蓟的磅数:");
scanf("%lf", £a);
suma += pounda;
break;
case 'b':printf("输入甜菜的磅数:");
scanf("%lf", £b);
sumb += poundb;
break;
case 'c':printf("输入胡萝卜的磅数:");
scanf("%lf", £c);
sumc += poundc;
break;
case 'q':goto quit;
case 'N':goto NEXT;
default :++n;
if(n%3 == 0)
goto quit;
else printf("输入失败\n失败三次将结束程序\n请输入a~c,退出请输入q\n");
continue; }
printf("继续输入,按q退出, 按N进入下一步\n");
m = getchar();
continue;
NEXT:break;}
printf("共订购洋蓟%lf磅,甜菜%lf磅,胡萝卜%lf磅\n");
price = suma*2.05 + sumb*1.15 + sumc*1.09;
if (price > 100)
price *= 0.95;
sumpound = suma + sumb +sumc;
if (sumpound <= 5)
tra_pac_cost = 6.5;
else if (sumpound > 5 && sumpound < 20)
tra_pac_cost = 14;
else tra_pac_cost = (sumpound - 20)*0.5 +14;
printf("订购费用:%lf\n订购重量:%lf\n运费与包装费:%lf\n费用总额:%lf\n", price, sumpound, tra_pac_cost, price + tra_pac_cost);
quit :printf("Bye!");
return 0;
}
第14行的getchar()获得一个字符之后,缓冲区还有一个字符'\n',所以你在第38行的getchar()获得到的字符是'\n'
解决办法就是在38 行之前在添加一句getchar()用于清除那个回车字符'\n'
|