# Someone Help me with this code!! (Closed)

/*What I want the code to do is calculate the net-total of all gifts brought and then take the Total as an Input to automatically apply 15 percent discount in it, now algebraically-adding all the gifts work fine but It just shows the discounted total price as 0 why ? (sorry for the bad english) */ #include <stdio.h> int main () { float gift1, gift2, gift3, total, discount; // Defines the Variables to hold. printf("How much do you want to spend on your dad?"); scanf ("%f", &gift1); printf("How much do you want to spend on your mom?"); scanf("%f", &gift2); printf("How much do you want to spend on your brother?"); scanf("%f", &gift3); total = gift1+gift2+gift3; // Calculates the algebraic addition to get the the total value. printf ("\n Total of all that would be %.f", total); scanf("%.f", &total); // Gets the "total" value for input. total = 0.85 * discount; // Getting a 15% discount in total value. printf("\n If you get a discount of 15 percent on that you would still "); printf("have to pay %.f", total); return 0; }

6/14/2021 6:40:34 PM

Hemang Mitra30 Answers

New AnswerNo like, the total price after discount cannot be 0, unless we input all the first three variables as 0

we should not need to input fourth value, the "total' variable is taken as the input to apply discount on it.

Well what I want the code to do is simple, first it will ask for the first three variables labeled as gift1, gidt2 and gift3 after you input the value then it will add cost of all gifts then, again it will automatically take input from that total cost and apply a discount of 15% and then give you the discounted cost.

Visph I tried the same code in other ides and locally in my computer, the compiler are not giving the same output as the one in sololeran how ?

Hemang Mitra sorry I am unable to help you in fully. May be I was unable to get your question. Sorry for thay

Your mistake in the code was that discount was not assigned with value. And you were doing product of 0.85 and discount to get total.Thus you got 0 as output.Actually no need of discount variable. Well, Good qn bro.๐ I'll post my soln code soon.๐ https://code.sololearn.com/cS4SXy7p2uYj/?ref=app

SREEHARI A H Haha yes I later changed that too. delete the line 14 to get not take input in between. As told by vishp then discount= 0.85 * total; printf("\n If you get a discount of 15 percent on that you would still") printf("have to pay %.2f", discount); return 0;

actually you mean to take 85% of total to print as 15% is discount ... then discount = total * 0.15; total= total - discount; printf("%f",total); this is the meaning I get from your question๐

Chinmay Anand Yes, it's right Just remove the line 14 from it, I realized that's the problem scanf, It isn't getting the "total" value for input automatically but rather asking us for the input. So after we give the input for 3 variables it will then ask for us to input the "total" and then discount that, not the total it got by adding all the three variables. Note:- This problems is for only those compiling in standalone multipass compilers(locally in their system mostly) , and sololearn doesn't have the same problem, Because it takes the Inputs first so if we input four variables instead of three it will add the first three then discount the fourth one giving us wrong output in the end. Try it

https://code.sololearn.com/ca94a12A8a17 #include <stdio.h> int main () { float gift1, gift2, gift3, total, discount=15; // Defines the Variables to hold. printf("How much do you want to spend on your dad?"); scanf ("%f", &gift1); printf("How much do you want to spend on your mom?"); scanf("%f", &gift2); printf("How much do you want to spend on your brother?"); scanf("%f", &gift3); total = gift1+gift2+gift3; // Calculates the algebraic addition to get the the total value. printf ("\n Total of all that would be %.f", total); scanf("%.f", &total); // Gets the "total" value for input. total = total- (total * discount)/100; // Getting a 15% discount in total value. printf("\n If you get a discount of 15 percent on that you would still "); printf("have to pay %.f", total); return 0; }