while循环中内容重复printf2次的解决办法

8 篇文章 0 订阅
3 篇文章 0 订阅

        while循环中内容之所以重复打印2次,是回车键(\n)作祟

        c语言中输入字符后储存在缓冲区,输入完毕后按回车键后执行,回车键(\n)此时被视为字符,同样进入while循环验证真伪,造成printf重复执行了两次,下面以c primer plus 第七章7.12.11题为例演示解决办法

# include <stdio.h>
# define ARTICHOKE 2.05
# define BEETROOT 1.15
# define CARROT 1.09
# define DISCOUNT 0.05
# define PACKAGE1 6.5
# define PACKAGE2 14
# define PACKAGE3 0.5
//printf语句会执行两次,sum质量,sum1洋jian质量,sum2甜菜质量,sum3胡萝卜质量,sum4运费包装费,sum5未折扣钱数,sum6蔬菜钱数,sum7折后钱数 

int main(void)
{
	float i, j, k, sum, sum1, sum2, sum3, sum4, sum5, sum6, sum7;
	char ch;
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	do
	{	
		scanf("%c", &ch);
	if(ch != 'q')
		switch (ch)
	{
		case 'a':
			{
			printf("please enter the pound\n");
			scanf("%f", &i);
			sum1 += i;
			break;
			}
		case 'b':
	 		{
			printf("please enter the pound\n");
			scanf("%f", &j);
			sum2 += j;
			break;
			}
		case 'c':
			{
			printf("please enter the pound\n");
			scanf("%f", &k);
			sum3 += k;
			break;
			}				
	}
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	}while(ch != 'q');
	sum = sum1 + sum2 + sum3;
	if(sum <= 5)
		sum4 = PACKAGE1;
	else if(sum <= 20)
		sum4 = PACKAGE2;
	else if(sum > 20)
		sum4 = PACKAGE2	+ (sum - 20) * PACKAGE3;
	sum6 = sum1 * ARTICHOKE + sum2 * BEETROOT + sum3 * CARROT;
	sum5 = sum6 + sum4;
	
	if(sum >= 100)
		sum7 = sum5 - sum5 * DISCOUNT;
	printf("artichoke is %f dollars/pound\nbeetroot is %f dollars/pound\ncarrot is %f dollars/pounds\n", ARTICHOKE, BEETROOT, CARROT);
	if(sum > 0)
		printf("the total weight is %.2f pound\n", sum);
	if(sum6 > 0)
		printf("the vegetable price is %.2f dollars\n", sum6);
	if(sum7 > 0)	
		printf("the total price is %.2f dollars\n", sum7);
	if(sum5 > 100)
		printf("the discount dollars is %.2f\n", sum5 - sum7);
	if(sum4 > 0)
		printf("the package is %.2f dollars\n", sum4);
	return 0;
}

运行结果示例

可见please select the vegetable执行了两次

解决问题的办法则是在do while循环中加入

while(getchar()!='\n')

        continue;

//消耗掉\n(具体内容视情况修改)

以下是修改后的代码段以及运行结果

# include <stdio.h>
# define ARTICHOKE 2.05
# define BEETROOT 1.15
# define CARROT 1.09
# define DISCOUNT 0.05
# define PACKAGE1 6.5
# define PACKAGE2 14
# define PACKAGE3 0.5
//sum质量,sum1洋jian质量,sum2甜菜质量,sum3胡萝卜质量,sum4运费包装费,sum5未折扣钱数,sum6蔬菜钱数,sum7折后钱数 

int main(void)
{
	float i, j, k, sum, sum1, sum2, sum3, sum4, sum5, sum6, sum7;
	char ch;
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	do
	{	
		scanf("%c", &ch);
	if(ch != 'q')
		switch (ch)
	{
		case 'a':
			{
			printf("please enter the pound\n");
			scanf("%f", &i);
			sum1 += i;
			break;
			}
		case 'b':
	 		{
			printf("please enter the pound\n");
			scanf("%f", &j);
			sum2 += j;
			break;
			}
		case 'c':
			{
			printf("please enter the pound\n");
			scanf("%f", &k);
			sum3 += k;
			break;
			}				
	}
	while(getchar() != '\n')
		continue;
	printf("please select the vegetable\na is artichoke\nb is beetroot\nc is carrot\nq is quit\n");
	}while(ch != 'q');
	sum = sum1 + sum2 + sum3;
	if(sum <= 5)
		sum4 = PACKAGE1;
	else if(sum <= 20)
		sum4 = PACKAGE2;
	else if(sum > 20)
		sum4 = PACKAGE2	+ (sum - 20) * PACKAGE3;
	sum6 = sum1 * ARTICHOKE + sum2 * BEETROOT + sum3 * CARROT;
	sum5 = sum6 + sum4;
	
	if(sum >= 100)
		sum7 = sum5 - sum5 * DISCOUNT;
	printf("artichoke is %f dollars/pound\nbeetroot is %f dollars/pound\ncarrot is %f dollars/pounds\n", ARTICHOKE, BEETROOT, CARROT);
	if(sum > 0)
		printf("the total weight is %.2f pound\n", sum);
	if(sum6 > 0)
		printf("the vegetable price is %.2f dollars\n", sum6);
	if(sum7 > 0)	
		printf("the total price is %.2f dollars\n", sum7);
	if(sum5 > 100)
		printf("the discount dollars is %.2f\n", sum5 - sum7);
	if(sum4 > 0)
		printf("the package is %.2f dollars\n", sum4);
	return 0;
}

问题得到解决

欢迎指正,如有其他更好的办法欢迎探讨交流 

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值