关于c语言缓冲区空格的问题
写作业的时候,出了一点问题,使用getchar接受输入的时候,会带有一个换行,然后就想到小甲鱼老师说的getchar()接受空格,但使用后并不理想,想问问是我放置不对吗?也尝试了一下在switch中case一个'\n' 然后break ,也实现不了,所以来论坛求助一下,#include<stdio.h>#include<stdlib.h>
#define Yan 2.05
#define Tia 1.15
#define Hul 1.09
int main(void)
{
int sum1,sum2,sum3;
int poundage1,poundage2,poundage3;
double prise1,prise2,prise3;
char ch;
do
{
printf("请输入您本次需要订购的产品('q' 退出累加)\n");
ch = getchar();
getchar();
switch(ch)
{
case 'a':
printf("请输入洋葱的磅数:\n");
scanf("%d",£age1);
sum1 = sum1 + poundage1;
break;
case 'b':
printf("请输入甜菜的磅数:\n");
scanf("%d",£age2);
sum2 = sum2 + poundage2;
break;
case 'c':
printf("请输入胡萝卜的磅数:\n");
scanf("%d",£age3);
sum3 = sum3 + poundage3;
break;
case 'q':
goto Tiaotiao;
break;
}
}while(ch != 'q');
Tiaotiao:;
int poundage;
poundage = poundage1 + poundage2 + poundage3;//sum int
prise1 = (poundage1 * Yan) + (poundage2 * Tia) + (poundage3 * Hul);//money sum dpuble
if(poundage < 100)
{
prise1 = prise1 - (prise1 * 0.05);
}
//运费
double freight;
if(poundage <= 5){
freight = 6.5;}
else if(poundage <= 15){
freight = 14.0;}
else{
freight = 14 + (20 - poundage) * 0.5;}
printf("poundage = %d",poundage);
printf("\nprise = %.2f",prise1);
printf("\nfreight = %.2f",freight);
printf("\nsum = freight + prise1 = %.2f\n",prise1 + freight);
return 0;
}代码如上,问题出现在do while循环里的接受字符那里。xx 这是图片 本帖最后由 superbe 于 2020-4-24 18:20 编辑
case 'q':
goto Tiaotiao;
break;
}
getchar();//添加这一行, 用来接收输入磅数后的回车
}while(ch != 'q'); superbe 发表于 2020-4-24 18:18
case 'q':
goto Tiaotiao;
break;
好的,谢谢了,我想问一下,我这个程序因为使用了浮点数,所以精度不够,这种情况应该怎么办呢
页:
[1]