【C语言】 缓冲区问题 循环使用getchar scanf会受到换行符'\n'的干扰

有一天,一个普通的练习:

#include<stdio.h>
int main(){
    float price_1=1.25,price_2=0.65,price_3=0.89;
    float quantity_1=0.0,quantity_2=0.0,quantity_3=0.0;
    float total_price=0.0,total_quantity=0.0;
    float total_price_end=0.0,preferential_price=0.0,freight=0.0,Price=0.0;
    char ch;
    while (ch!='q') {
        printf("欢迎来到文哲商店!~在下面选择你喜爱的商品,按q确认\n");
        printf("a)朝鲜鱼 1.25美元/磅\nb)甜菜 0.65美元/磅\nc)胡萝卜 0.89美元/磅\n");
        scanf("%c",&ch);
        switch (ch) {
            case 'a':
                printf("你的最终数量为:%f\n请输入朝鲜鱼的数量:",quantity_1);
                scanf("%f",&quantity_1);
                getchar();
                break;
            case 'b':
                printf("你的最终数量为:%f\n请输入甜菜的数量:",quantity_2);
                scanf("%f",&quantity_2);
                getchar();
                break;
            case 'c':
                printf("你的最终数量为:%f\n请输入胡萝卜的数量:",quantity_3);
                scanf("%f",&quantity_3);
                getchar();
                break;
            default:
                break;
        }
    }
    printf("你选择%f磅朝鲜鱼,共%f美元\n",quantity_1,quantity_1*price_1);
    printf("你选择%f磅甜菜,共%f美元\n",quantity_2,quantity_2*price_2);
    printf("你选择%f磅胡萝卜,共%f美元\n",quantity_3,quantity_3*price_3);
    total_price=price_1*quantity_1+price_2*quantity_2+price_3*quantity_3;
    total_quantity=quantity_1+quantity_2+quantity_3;
    if (total_price>=100) {
        printf("*你的总价为:%f,超过了100美元,可享受95折优惠\n",total_price);
        total_price_end=total_price*0.95;
        preferential_price=total_price-total_price_end;
    }else{
        printf("*你的总价为:%f,没有超过100美元,无优惠\n",total_price);
        total_price_end=total_price;
        preferential_price=0.0;
    }
    printf("*你的优惠价格为:%f\n",preferential_price);
    printf("下面开始计算运费\n*你的总磅数是:%f\n",total_quantity);
    if (total_quantity<=5) {
        freight=3.5;
    }else if(total_quantity<20){
        freight=10;
    }else{
        freight=8+total_quantity*0.1;
    }
    printf("*你的运输费用为:%f\n",freight);
    Price=total_price_end+freight;
    printf("**********\n你的总费用为%f\n",Price);
    
    return 0;

}


模拟购物商店,但是却发生了意想不到的事:

在前面的while语句中,出现了循环两次的情况。


仔细检查代码,并没有语法错误。

于是,我就回到寝室和小强(大牛)请教。

他也好久没碰c了,首先值得肯定的是,语法是没有问题。我们打了注释,发现和switch,printf,getchar,while这些都没关系,可以肯定的是问题还是出自scanf,我们将问题归结为可能是操作系统或是C语言的底层

我用的mac系统,是在Xcode上码的,我把代码发到他的VS上,期待的看着打印结果,~~却发现也有同样的问题。

于是我们加了C++的头文件,稍稍修改,发现果然C++并没有这个问题!

为了找出这个底层的原因,我们开始断点,发现,输入11之后,再次循环while时,scanf没有提示输入直接跳过,而此时ch由‘a’变成了‘\n’。正是因为这个‘\n’,使主菜单打印了两次

上网搜索这个换行符究竟什么来头,查到了“缓冲区”这个概念

并说及时刷新缓冲区可以解决这个问题

一种跨平台的方法 getchar()

#include<stdio.h>
int main()
{
    float price_1=1.25,price_2=0.65,price_3=0.89;
    float quantity_1=0.0,quantity_2=0.0,quantity_3=0.0;
    float total_price=0.0,total_quantity=0.0;
    float total_price_end=0.0,preferential_price=0.0,freight=0.0,Price=0.0;
    char ch;
    while (ch!='q') {
        printf("欢迎来到文哲商店!~在下面选择你喜爱的商品,按q确认\n");
        printf("a)朝鲜鱼 1.25美元/磅\nb)甜菜 0.65美元/磅\nc)胡萝卜 0.89美元/磅\n");
        scanf("%c",&ch);
        switch (ch) {
            case 'a':
                printf("你的最终数量为:%f\n请输入朝鲜鱼的数量:",quantity_1);
                scanf("%f",&quantity_1);
                break;
            case 'b':
                printf("你的最终数量为:%f\n请输入甜菜的数量:",quantity_2);
                scanf("%f",&quantity_2);
                break;
            case 'c':
                printf("你的最终数量为:%f\n请输入胡萝卜的数量:",quantity_3);
                scanf("%f",&quantity_3);
                break;
            default:
                break;
        }
        getchar();
        //fflush(stdin);//刷新缓冲区
    }
    
    printf("你选择%f磅朝鲜鱼,共%f美元\n",quantity_1,quantity_1*price_1);
    printf("你选择%f磅甜菜,共%f美元\n",quantity_2,quantity_2*price_2);
    printf("你选择%f磅胡萝卜,共%f美元\n",quantity_3,quantity_3*price_3);
    total_price=price_1*quantity_1+price_2*quantity_2+price_3*quantity_3;
    total_quantity=quantity_1+quantity_2+quantity_3;
    if (total_price>=100) {
        printf("*你的总价为:%f,超过了100美元,可享受95折优惠\n",total_price);
        total_price_end=total_price*0.95;
        preferential_price=total_price-total_price_end;
    }else{
        printf("*你的总价为:%f,没有超过100美元,无优惠\n",total_price);
        total_price_end=total_price;
        preferential_price=0.0;
    }
    printf("*你的优惠价格为:%f\n",preferential_price);
    printf("下面开始计算运费\n*你的总磅数是:%f\n",total_quantity);
    if (total_quantity<=5) {
        freight=3.5;
    }else if(total_quantity<20){
        freight=10;
    }else{
        freight=8+total_quantity*0.1;
    }
    printf("*你的运输费用为:%f\n",freight);
    Price=total_price_end+freight;
    printf("**********\n你的总费用为%f\n",Price);
    
    return 0;
}
如上,解决了那个的问题。

第二天,我想既然是c才会有的问题,我便翻开那本老朋友《C Primer Plus》

果然有谈到“缓冲区”!

上面谈到的知识点大致如下:

缓冲分为两类:完全缓冲和行缓冲。

对完全缓冲输入来说,缓冲区满时被清空。这总类型的缓冲通常出现在文件输入中。缓冲区的大小取决于系统,但512字节和4096字节是常见的值。

对行缓冲输入来说,遇到一个换行字符时被清空缓冲区。键盘输入是标准的行缓冲,因此按下回车键将清空缓冲区。

文件结尾(EOF)换行符(\n)

针对这个问题,书上的解决方案是

while(getchar()!='\n'){
    continue;
}


啊! 先研究到这,我要去玩滑板了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值