Home > Back-end >  C language don't understand
C language don't understand

Time:01-07

 # 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 debugging

CodePudding user response:

Before the effective input with % c, need to carefully clean up the input buffer, general use
while(getchar()!='\ n');
Line,

CodePudding user response:

reference 1/f, a white steamed bun response:
actually 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, is the default situation,

Why isn't this value must be A.B.C

CodePudding user response:

The
references a white steamed bread on the second floor 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 debugging

haven't learned to interrupt

CodePudding user response:

reference 5 floor xiangyang's reply:
Quote: refer to the second floor of a white steamed bun reply:
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 debugging

haven't learned to interrupt point

can learn to debug the code, because after you enter a character, you also knocked a carriage return, this is also one character at a time, so the second scanf_s may be read the carriage return this character, specific debugging are also not sure, but since scanf_s it again, you ch value must be changed

CodePudding user response:

quoted zhao 4, 4/f, the teacher's reply:
behind every last without \ n printf plus fflush (stdout);
In each don't want to receive buffer in front of the old content affected the scanf and rewind (stdin);
In addition, please check the return values of the scanf
//in the future, please use 
char c;
The scanf (" % c ", & amp; C);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related