# include & lt; stdio.h>
# define beet 2.05
# define artichokes 1.15
# define have 1.09
Int main ()
{
Int I, k=0;
Double total_price_goods, discount=0, freight_packingExpense, total_order_cost, beet_pound=0, artichokes_pound=0, carrot_pound=0;
Double total_price_beet total_price_artichokes, total_price_carrots, a, b, c, total_pound;
Char ch;
Printf (" ABC mail-order grocery store ordering system \ n ");
Beet artichokes printf (" a) b) c) carrot q) exit \ n ");
While (1)
{
If (k!
=1)Printf (" please enter your purchase goods number: ");
The else
K=0;
Scanf_s (" % c ", & amp; Ch. 1);
The switch (ch)
{
Case 'a' :
Printf (" please enter the purchase artichokes pounds: ");
Lf scanf_s (" % ", & amp; A);
Artichokes_pound +=a;
break;
Case 'b' :
Printf (" please enter to buy beet pounds: ");
Lf scanf_s (" % ", & amp; B);
Beet_pound +=b;
break;
Case 'c' :
Printf (" please enter the purchase of carrot pounds: ");
Lf scanf_s (" % ", & amp; C);
Carrot_pound +=c;
break;
Case 'q:
break;
Default:
Printf (" please input the correct number: \ n ");
K=1;
continue;
}
If (ch=='q')
break;
}
Total_price_artichokes=artichokes_pound * artichokes;
Total_price_beet=beet * beet_pound;
Total_price_carrots=carrot_pound * have;
Total_price_goods=total_price_artichokes + total_price_beet + total_price_carrots;
If (total_price_goods & gt;
=100)Discount=total_price_goods * 0.05;
Total_pound=beet_pound + artichokes_pound + carrot_pound;
If (total_pound==0)
Freight_packingExpense=0;
The else
If (total_pound & lt;
=5)Freight_packingExpense=6.5;
Else if (total_pound & lt;=20)
Freight_packingExpense=14;
The else
Freight_packingExpense=14 + (total_pound - 20) * 0.5;
Total_order_cost=total_price_goods + freight_packingExpense - discount;
For (I=1; I & lt;=37; I++)
Printf (" * ");
Printf (" order ");
For (I=1; I & lt; 37. I++)
Printf (" * ");
printf("\n");
Printf (" item price order weight (unit: pounds) items cost \ n ");
Printf (" a artichokes % % 2 f, 2 f. % 2 f \ n ", artichokes, artichokes_pound, total_price_artichokes);
Printf (" % b beets. % 2 f. % 2 f, 2 f \ n ", beet, beet_pound, total_price_beet);
Printf (" % c carrots. % 2 f. % 2 f, 2 f \ n ", have, carrot_pound, total_price_carrots);
Printf (" order total cost: % 2 f % discount. % 2 f freight and packing. The total cost of 2 f: %, 2 f \ n ", total_price_goods, discount, freight_packingExpense, total_order_cost);
For (I=1; I & lt; 80; I++)
Printf (" * ");
printf("\n");
return 0;
}
Photos first why will have input the correct number, why the second place, there are two input the correct Numbers to solve?? Novice, just learning c 1 month
CodePudding user response:
In fact this problem out by logic, start with your input 10,At this time of the switch structure has quit, and then the if (ch=='q') this sentence is not established, so the while loop didn't quit, began to cycle, because your k value unchanged, so perform the printf (" please enter a number: do you want to buy goods "); This sentence, scanf_s (" % c ", & amp; Ch. 1); Then this sentence again for an input, the value of the ch is definitely not a, b, c, and so entered the default: execute the printf (" please input the correct number: \ n "); This sentence, you enter the d finally, and the case of default,
CodePudding user response:
No manual debugging, scanf_s (" % c ", & amp; Ch. 1); This sentence for the second time to obtain the input should be input when you enter, Suggestions for interrupting point debuggingCodePudding user response:
Before the effective input with % c, need to carefully clean up the input buffer, general usewhile(getchar()!='\ n');
Line,
CodePudding user response: