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

        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;
}

运行结果示例

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

可见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;
}

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

问题得到解决

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

上一篇:杭电1879继续畅通project


下一篇:Where Did the Other Dollar Go, Jeff?