while(scanf("%c",&ch)&&ch!='q')
{
switch(ch)
{
case 'a':
scanf("%lf",&d);
cost1=YY*d;
break;
case 'b':
scanf("%lf",&e);
cost2=TC*e;
break;
case 'c':
scanf("%lf",&f);
cost3=HLB*f;
break;
default:
printf("write right number pls");
}
}
switch里的case语句这样写可以吧,但为什么先输入a之后没办法给d赋值啊?
你while语句里有scanf啊,先输入判断能不能循环。
我这边是可以赋值的啊,只是回车之后一定会输出default里的一句话,需要避免的话可以使用getchar()吃掉回车
while (scanf("%c", &ch) && ch != 'q')
{
switch (ch)
{
case 'a':
scanf("%lf", &d);
getchar();
cost1 = YY * d;
break;
case 'b':
scanf("%lf", &e);
getchar();
cost2 = TC * e;
break;
case 'c':
scanf("%lf", &f);
getchar();
cost3 = HLB * f;
break;
default:
printf("write right number pls");
}
}
while (ch != 'q')
{
scanf("%c", &ch);
switch (ch)
{
case 'a':
scanf("%lf\n", &d);
cost1 = YY * d;
break;
case 'b':
scanf("%lf\n", &e);
cost2 = TC * e;
break;
case 'c':
scanf("%lf\n", &f);
cost3 = HLB * f;
break;
default:
printf("write right number pls\n");
break;
}
}