标题:输入字母后如何必需回车确认呢
只看楼主
小小熊宝宝
Rank: 2
来 自:江苏无锡
等 级:论坛游民
威 望:2
帖 子:183
专家分:38
注 册:2008-8-12
结帖率:100%
已结贴  问题点数:2 回复次数:3 
输入字母后如何必需回车确认呢
程序代码:
#include<stdio.h>
#define artichoke 1.25
#define beet 0.65
#define carrot 0.89
#define pound_5 3.50
#define pound_5_20 10.00
#define pound_20 8.00
double judge(double i);
double judge(double i)
{
    while(1)
    {
        while(scanf("%lf",&i)!=1)
        {
            fflush(stdin);
            printf("格式错误,重新输入\n");
        }
        if(i<0)
            printf("磅数错误,重新输入\n");
        else
            break;
    }
    return i;
}
int main(void)
{
    double amount,favorable,total_pound,artichoke_pound,beet_pound,carrot_pound,freight;
    artichoke_pound=beet_pound=carrot_pound=0.0;
    char ch;
    printf("请选择您要下的订单,输入a为朝鲜蓟订单,b为甜菜订单,c为胡萝卜订单,q退出订单系统\n");
    ch=getchar();
    while(ch!='q')
    {
        switch(ch)
        {
        case 'a':printf("欢迎订购朝鲜蓟,请输入您要的磅数\n");
            artichoke_pound=judge(artichoke_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        case 'b':printf("欢迎订购甜菜,请输入您要的磅数\n");
            beet_pound=judge(beet_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        case 'c':printf("欢迎订购胡萝卜,请输入您要的磅数\n");
            carrot_pound=judge(carrot_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        default:
            printf("无此项订单,请重新选择\n");
            fflush(stdin);
            ch=getchar();
        }
    }
    total_pound=artichoke_pound+beet_pound+carrot_pound;
    favorable=(int)total_pound/100;
    if(total_pound-20<=-15)
    {
        freight=pound_5;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else if(total_pound-20>-15&&total_pound-20<=0)
    {
        freight=pound_5_20;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else
    {
        freight=(int)total_pound/1*0.1+8.00;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    printf("您订购了%.2lf磅朝鲜蓟,%.2lf磅甜菜,%.2lf磅胡萝卜,总计%.2lf磅。\n",artichoke_pound,beet_pound,carrot_pound,total_pound);
    printf("您需要支付运输和装卸费用$ %.2lf,您可以获得$ %.2lf优惠,总费用为$ %.2lf\n",freight,favorable,amount);
        return 0;
}
再输入a20的情况下,会把后面的数据提前录入,输入字母后如何必需回车确认呢
搜索更多相关主题的帖子: 输入 回车 字母 
2010-09-30 13:13
w250681965
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2010-9-3
得分:2 
程序代码:
#include<stdio.h>
#define artichoke 1.25
#define beet 0.65
#define carrot 0.89
#define pound_5 3.50
#define pound_5_20 10.00
#define pound_20 8.00
double judge(double i);
double judge(double i)
{
    while(1)
    {
        if(scanf("%lf",&i)!=1)
        {
           // fflush(stdin);
            printf("格式错误,重新输入\n");
        }
       else if(i<0)                                            //你这里如果<0的话是死循环
            printf("磅数错误,重新输入\n");
        else
            break;
    }
    return i;
}
int main(void)
{
    double amount,favorable,total_pound,artichoke_pound,beet_pound,carrot_pound,freight;
    char ch;
    char enter;
    artichoke_pound=beet_pound=carrot_pound=0.0;
    printf("请选择您要下的订单,输入a为朝鲜蓟订单,b为甜菜订单,c为胡萝卜订单,q退出订单系统\n");
    ch=getchar();
    while((enter=getchar())!='\n')                //定义一个确认输入字符enter
    {
        printf("请重新选择,并Enter确认!\n");
        fflush(stdin);
        ch=getchar();
    }
    while(ch!='q')
    {
        switch(ch)
        {
        case 'a':printf("欢迎订购朝鲜蓟,请输入您要的磅数\n");
            artichoke_pound=judge(artichoke_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        case 'b':printf("欢迎订购甜菜,请输入您要的磅数\n");
            beet_pound=judge(beet_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        case 'c':printf("欢迎订购胡萝卜,请输入您要的磅数\n");
            carrot_pound=judge(carrot_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            break;
        default:
            printf("无此项订单,请重新选择\n");
            fflush(stdin);
            ch=getchar();
        }
    }
    total_pound=artichoke_pound+beet_pound+carrot_pound;
    favorable=(int)total_pound/100;
    if(total_pound-20<=-15)
    {
        freight=pound_5;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else if(total_pound-20>-15&&total_pound-20<=0)
    {
        freight=pound_5_20;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else
    {
        freight=(int)total_pound/1*0.1+8.00;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    printf("您订购了%.2lf磅朝鲜蓟,%.2lf磅甜菜,%.2lf磅胡萝卜,总计%.2lf磅。\n",artichoke_pound,beet_pound,carrot_pound,total_pound);
    printf("您需要支付运输和装卸费用$ %.2lf,您可以获得$ %.2lf优惠,总费用为$ %.2lf\n",freight,favorable,amount);
    return 0;
}


第一轮正常 后面第二次选择还是这个问题!用switch中都要判断第二次输入是否为'\n'
2010-09-30 16:01
小小熊宝宝
Rank: 2
来 自:江苏无锡
等 级:论坛游民
威 望:2
帖 子:183
专家分:38
注 册:2008-8-12
得分:0 
3q

欢迎加入C语言交流群:43287755
2010-09-30 16:11
小小熊宝宝
Rank: 2
来 自:江苏无锡
等 级:论坛游民
威 望:2
帖 子:183
专家分:38
注 册:2008-8-12
得分:0 
程序代码:
#include<stdio.h>
#define artichoke 1.25
#define beet 0.65
#define carrot 0.89
#define pound_5 3.50
#define pound_5_20 10.00
#define pound_20 8.00
double judge(double i);
double judge(double i)
{
    while(1)
    {
        if(scanf("%lf",&i)!=1)
        {
            printf("格式错误,重新输入\n");
        }
       else if(i<=0)
            printf("磅数错误,重新输入\n");
        else
            break;
    }
    return i;
}
int main(void)
{
    double amount,favorable,total_pound,artichoke_pound,beet_pound,carrot_pound,freight;
    char ch;
    char enter;
    artichoke_pound=beet_pound=carrot_pound=0.0;
    printf("请选择您要下的订单,输入a为朝鲜蓟订单,b为甜菜订单,c为胡萝卜订单,q退出订单系统\n");
    ch=getchar();
    while((enter=getchar())!='\n')                //定义一个确认输入字符enter
    {
        printf("请重新选择,并Enter确认!\n");
        fflush(stdin);
        ch=getchar();
    }
    while(ch!='q')
    {
        switch(ch)
        {
        case 'a':printf("欢迎订购朝鲜蓟,请输入您要的磅数\n");
            artichoke_pound=judge(artichoke_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();
            while((enter=getchar())!='\n')                //定义一个确认输入字符enter
            {
                printf("请重新选择,并Enter确认!\n");
                fflush(stdin);
                ch=getchar();
            }
            break;
        case 'b':printf("欢迎订购甜菜,请输入您要的磅数\n");
            beet_pound=judge(beet_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();           
            while((enter=getchar())!='\n')                //定义一个确认输入字符enter
            {
                printf("请重新选择,并Enter确认!\n");
                fflush(stdin);
                ch=getchar();
            }
            break;
        case 'c':printf("欢迎订购胡萝卜,请输入您要的磅数\n");
            carrot_pound=judge(carrot_pound);
            printf("请选择您要下的订单,按q退出订单系统并计算费用\n");
            fflush(stdin);
            ch=getchar();           
            while((enter=getchar())!='\n')                //定义一个确认输入字符enter
            {
                printf("请重新选择,并Enter确认!\n");
                fflush(stdin);
                ch=getchar();
            }
            break;
        default:
            printf("无此项订单,请重新选择\n");
            fflush(stdin);
            ch=getchar();           
            while((enter=getchar())!='\n')                //定义一个确认输入字符enter
            {
                printf("请重新选择,并Enter确认!\n");
                fflush(stdin);
                ch=getchar();
            }
        }
    }
    total_pound=artichoke_pound+beet_pound+carrot_pound;
    favorable=(int)total_pound/100;
    if(total_pound-20<=-15)
    {
        freight=pound_5;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else if(total_pound-20>-15&&total_pound-20<=0)
    {
        freight=pound_5_20;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    else
    {
        freight=(int)total_pound/1*0.1+8.00;
        amount=artichoke_pound*artichoke+beet_pound*beet+carrot_pound*carrot-favorable*0.05+freight;
    }
    printf("您订购了%.2lf磅朝鲜蓟,%.2lf磅甜菜,%.2lf磅胡萝卜,总计%.2lf磅。\n",artichoke_pound,beet_pound,carrot_pound,total_pound);
    printf("您需要支付运输和装卸费用$ %.2lf,您可以获得$ %.2lf优惠,总费用为$ %.2lf\n",freight,favorable,amount);
    return 0;
}

欢迎加入C语言交流群:43287755
2010-09-30 16:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321749-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027455 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved