user-0 提问: 新人，求大神看看代码！

回复:
user: user-1
content: 1.变量最好都初始化一下 2.scanf("%c", &ab);最好清空一下输入缓冲区，否则可能会直接读入残留的回车符  #include<stdio.h> #define A 1.25 #define B 0.65 #define C 0.89 int main(void) { 	char ch, ab; 	float d = 0.0, e = 0.0, f = 0.0, g = 0.0, h = 0.0, i = 0.0, j = 0.0; 	printf("Welcome to ABC Mail Order Grocery!Please choose you farvorite:\n"); 	printf("a) thistle(1.25\\pound) b) beet(0.65\\pound) c) carrot(0.89\\pound) \n"); 	printf("q) quit\n"); 	while ((ch = getchar()) != 'q') 	{ 		switch (ch) 		{ 		case 'a': 			scanf("%f", &d); 			e = d*A; 			printf("Are you finishi?(y or n)"); 			fflush(stdin); 			scanf("%c", &ab); 			if (ab == 'y') 				break; 			if (ab == 'n') 				continue; 		case 'b': 			scanf("%f", &f); 			g = f*B; 			printf("Are you finishi?(y or n)"); 			fflush(stdin); 			scanf("%c", &ab); 			if (ab == 'y') 				break; 			if (ab == 'n') 				continue; 		case 'c': 			scanf("%f", &h); 			i = h*C; 			printf("Are you finishi?(y or n)"); 			fflush(stdin); 			scanf("%c", &ab); 			if (ab == 'y') 				break; 			if (ab == 'n') 				continue; 		default: 			printf("Please enter the right word\n"); 		}  		break; 	} 	j = e + g + i; 		printf("%f", j);  	return 0; }

user: user-2
content: 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。 //请今后要用 int c; scanf("%c",&c); //时，都改为 char s[2]; int c; scanf("%1s",s); c=s[0];

user: user-0
content: 引用 3楼千行百行 的回复:连续读写时候要注意收回“回车”...不懂什么意思，为什么我之前那个会连续输出are you finish和please enter therightword 而在再加上getchar后不会重复输出

user: user-0
content: 引用 2楼千行百行 的回复:#include<stdio.h> #define A 1.25 #define B 0.65 #define C 0.89 int main(void) { 	char ch,ab; 	float d=0,e=0,f=0,g=0,h=0,i=0,j=0; 	printf("Welcome to ABC Mail Order Grocery! Please choose you farvorite:\n"); 	printf("a) thistle(1.25\\pound) b) beet(0.65\\pound) c) carrot(0.89\\pound) \n"); 	printf("q) quit\n"); 	while((ch=getchar())!='q') 	{ 		switch(ch) 		{ 			case 'a': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				e=d*A; 				printf("Are you finish? (y or n)"); 				scanf("%c",&ab); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			case 'b': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				g=d*B; 				printf("Are you finish? (y or n)"); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			case 'c': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				i=d*C; 				printf("Are you finish? (y or n)"); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			default: 				printf("Please enter the right word\n"); 		} 		break; 	} 	j=e+g+i; 	printf("%f\n",j); 	return 0; } 改成这样就好了，楼主试试看可以了！

user: user-3
content: 我写的还是比较复杂，给提点意见吧。

user: user-3
content: 连续读写时候要注意收回“回车”

user: user-3
content: #include<stdio.h> #define A 1.25 #define B 0.65 #define C 0.89 int main(void) { 	char ch,ab; 	float d=0,e=0,f=0,g=0,h=0,i=0,j=0; 	printf("Welcome to ABC Mail Order Grocery! Please choose you farvorite:\n"); 	printf("a) thistle(1.25\\pound) b) beet(0.65\\pound) c) carrot(0.89\\pound) \n"); 	printf("q) quit\n"); 	while((ch=getchar())!='q') 	{ 		switch(ch) 		{ 			case 'a': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				e=d*A; 				printf("Are you finish? (y or n)"); 				scanf("%c",&ab); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			case 'b': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				g=d*B; 				printf("Are you finish? (y or n)"); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			case 'c': 				//getchar(); 				scanf("%f",&d); 				getchar(); 				i=d*C; 				printf("Are you finish? (y or n)"); 				if(ab=='y') break; 				if(ab=='n')  				{ 					getchar(); 					continue; 				} 			default: 				printf("Please enter the right word\n"); 		} 		break; 	} 	j=e+g+i; 	printf("%f\n",j); 	return 0; } 改成这样就好了，楼主试试看

user: user-4
content: 你的break退出的是switch 你可以看看这个： http://zhidao.baidu.com/question/335566329.html