鱼C论坛

 找回密码
 立即注册
查看: 1846|回复: 5

[已解决]打印问题

[复制链接]
发表于 2019-4-8 15:01:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <ctype.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
int main(void)
{
        char ch;
        int sum_pound1 = 0, sum_pound2 = 0,sum_pound3 = 0;
        int pound1,pound2,pound3;
        printf("a) artichoke     b) beet\n");
        printf("c) carrot        q) quit\n");
        printf("Please enter the wodld.\n");

        while ((ch = getchar()) != 'q')
        {
                ch = tolower(ch);
                switch (ch)
                {
                case 'a':
                        printf("Please enter the pound of artichoke.\n");
                        scanf_s("%d", &pound1);
                        sum_pound1 += pound1;
                        break;
                case 'b':
                        printf("Please enter the pound of been.\n");
                        scanf_s("%d", &pound2);
                        sum_pound2 += pound2;
                        break;
                case 'c':
                        printf("Please enter the pound of carrot.\n");
                        scanf_s("%d", &pound3);
                        sum_pound3 += pound3;
                        break;
                default:
                        break;
                }
                printf("Please enter the world again.\n");
        }

        getchar();
        return 0;
}



这个程序为什么最后的printf会打印两次?
最佳答案
2019-4-8 22:23:27
因为你没有清空缓冲区,这样缓冲区内会剩一个'\n',
第二次就直接读入'\n'了,所以会额外循环一遍

在第二次读之前清空缓冲就好
#include <stdio.h>
#include <ctype.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
int main(void)
{
        char ch;
        int sum_pound1 = 0, sum_pound2 = 0,sum_pound3 = 0;
        int pound1,pound2,pound3;
        printf("a) artichoke     b) beet\n");
        printf("c) carrot        q) quit\n");
        printf("Please enter the wodld.\n");

        setbuf(stdin,NULL); 
        while ((ch = getchar()) != 'q')
        {
                switch (ch)
                {
                case 'a':
                        printf("Please enter the pound of artichoke.\n");
                        scanf_s("%d", £1);
                        sum_pound1 += pound1;
                        break;
                case 'b':
                        printf("Please enter the pound of been.\n");
                        scanf_s("%d", £2);
                        sum_pound2 += pound2;
                        break;
                case 'c':
                        printf("Please enter the pound of carrot.\n");
                        scanf_s("%d", £3);
                        sum_pound3 += pound3;
                        break;
                default:
                        break;
                }
                
                printf("Please enter the world again.\n");
                while ((ch=getchar())!='\n' && ch!=EOF); //清空缓冲区
                
        }

        getchar();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-4-8 21:13:43 | 显示全部楼层
哪一个printf打印两次呀,讲一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-8 21:15:17 | 显示全部楼层
刘邦 发表于 2019-4-8 21:13
哪一个printf打印两次呀,讲一下

最后一个printf(“Please enter the world again.\n”);这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-8 22:08:03 | 显示全部楼层
不好意思,试了很多次这个语句放哪都不行,都会打印两次,帮不上你了,建议你重写一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-8 22:23:27 | 显示全部楼层    本楼为最佳答案   
因为你没有清空缓冲区,这样缓冲区内会剩一个'\n',
第二次就直接读入'\n'了,所以会额外循环一遍

在第二次读之前清空缓冲就好
#include <stdio.h>
#include <ctype.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
int main(void)
{
        char ch;
        int sum_pound1 = 0, sum_pound2 = 0,sum_pound3 = 0;
        int pound1,pound2,pound3;
        printf("a) artichoke     b) beet\n");
        printf("c) carrot        q) quit\n");
        printf("Please enter the wodld.\n");

        setbuf(stdin,NULL); 
        while ((ch = getchar()) != 'q')
        {
                switch (ch)
                {
                case 'a':
                        printf("Please enter the pound of artichoke.\n");
                        scanf_s("%d", £1);
                        sum_pound1 += pound1;
                        break;
                case 'b':
                        printf("Please enter the pound of been.\n");
                        scanf_s("%d", £2);
                        sum_pound2 += pound2;
                        break;
                case 'c':
                        printf("Please enter the pound of carrot.\n");
                        scanf_s("%d", £3);
                        sum_pound3 += pound3;
                        break;
                default:
                        break;
                }
                
                printf("Please enter the world again.\n");
                while ((ch=getchar())!='\n' && ch!=EOF); //清空缓冲区
                
        }

        getchar();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-4-9 17:37:40 | 显示全部楼层
流弊呀兄弟
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 14:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表