|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写作业的时候,出了一点问题,使用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');
|
|