鱼C论坛

 找回密码
 立即注册
查看: 2271|回复: 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'了,所以会额外循环一遍

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

  14.         setbuf(stdin,NULL);
  15.         while ((ch = getchar()) != 'q')
  16.         {
  17.                 switch (ch)
  18.                 {
  19.                 case 'a':
  20.                         printf("Please enter the pound of artichoke.\n");
  21.                         scanf_s("%d", &pound1);
  22.                         sum_pound1 += pound1;
  23.                         break;
  24.                 case 'b':
  25.                         printf("Please enter the pound of been.\n");
  26.                         scanf_s("%d", &pound2);
  27.                         sum_pound2 += pound2;
  28.                         break;
  29.                 case 'c':
  30.                         printf("Please enter the pound of carrot.\n");
  31.                         scanf_s("%d", &pound3);
  32.                         sum_pound3 += pound3;
  33.                         break;
  34.                 default:
  35.                         break;
  36.                 }
  37.                
  38.                 printf("Please enter the world again.\n");
  39.                 while ((ch=getchar())!='\n' && ch!=EOF); //清空缓冲区
  40.                
  41.         }

  42.         getchar();
  43.         return 0;
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-8 21:13:43 | 显示全部楼层
哪一个printf打印两次呀,讲一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

最后一个printf(“Please enter the world again.\n”);这个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-8 22:08:03 | 显示全部楼层
不好意思,试了很多次这个语句放哪都不行,都会打印两次,帮不上你了,建议你重写一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

  14.         setbuf(stdin,NULL);
  15.         while ((ch = getchar()) != 'q')
  16.         {
  17.                 switch (ch)
  18.                 {
  19.                 case 'a':
  20.                         printf("Please enter the pound of artichoke.\n");
  21.                         scanf_s("%d", &pound1);
  22.                         sum_pound1 += pound1;
  23.                         break;
  24.                 case 'b':
  25.                         printf("Please enter the pound of been.\n");
  26.                         scanf_s("%d", &pound2);
  27.                         sum_pound2 += pound2;
  28.                         break;
  29.                 case 'c':
  30.                         printf("Please enter the pound of carrot.\n");
  31.                         scanf_s("%d", &pound3);
  32.                         sum_pound3 += pound3;
  33.                         break;
  34.                 default:
  35.                         break;
  36.                 }
  37.                
  38.                 printf("Please enter the world again.\n");
  39.                 while ((ch=getchar())!='\n' && ch!=EOF); //清空缓冲区
  40.                
  41.         }

  42.         getchar();
  43.         return 0;
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-4-9 17:37:40 | 显示全部楼层
流弊呀兄弟
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-1 08:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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