how to display total, tax, and subtotal in a receipt - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# how to display total, tax, and subtotal in a receipt

Posts: 10Member
i'm new to the C language, so i was trying to do this piece of code that add the quantity of all items purchased, the subTotal, and the salesTax. However, i can't seem to figure out why the code is jumping to the third item and doing this calculation only. I know that something is being done wrong, i just cannnot seem to figure it out.
[code]
#include
#include

int main () {
double tvPrice = 400.00;
double vcrPrice = 220.00;
double remoteControlPrice = 35.20;
double cdPlayerPrice = 300.00;

double tapeRecorderPrice = 150.00;
const double tax = 0.0825;
int quantity = 0;
double subTotal = 0;
double total = 0, salesTax;

printf("How Many TV's Were Sold? ");
scanf("%d", &quantity);
subTotal = quantity * tvPrice;

printf("How Many VCR's Were Sold? ");
scanf("%d", &quantity);

printf("How Many Remote Controllers Were Sold? ");
scanf("%d", &quantity);
total = subTotal + tax;
total = subTotal + tax;

printf("How Many CD's Were Sold? ");
scanf("%d", &quantity);

printf("How Many Tape Recorders Were Sold? ");
scanf("%d", &quantity);

subTotal = quantity * tvPrice;
//subTotal = quantity * vcrPrice;

//subTotal = quantity * remoteControlPrice;

//subTotal = quantity * cdPlayerPrice;

//subTotal = quantity * remoteControlPrice;

salesTax = subTotal * tax;

total = subTotal + salesTax;

printf ("%6s%18s%18s%20s
", "QTY", "DESCRIPTION", "UNIT PRICE","TOTAL PRICE");
printf ("---------------------------------------------------------------
");
printf ("%6d%18s%16.2lf%18.2lf
",quantity,"TV",tvPrice,subTotal);
printf ("%6d%18s%16.2lf%18.2lf
",quantity,"VCR",vcrPrice,subTotal);
printf ("%6d%18s%16.2lf%18.2lf
",quantity,"REMOTE CTRLR",remoteControlPrice,total);
printf ("%6d%18s%16.2lf%18.2lf
",quantity,"CD PLAYER",cdPlayerPrice,total);
printf ("%6d%18s%16.2lf%18.2lf
",quantity,"TAPE RECORDER",tapeRecorderPrice,total);

printf(" SUBTOTAL: %10.2lf
", subTotal);
printf(" TAX: %10.2lf
", salesTax);
printf(" TOTAL: %10.2lf
", total);

system("pause");
return 0;
}
[/code]

• Posts: 395Member
The basic trouble is that you are expecting quantity to somehow keep track of all the items. A single variable will only keep track of the last value assigned. There are a couple other issues, but they should be easily rectified once the quantity issue is resolved.

Take Care,
Ed
• Posts: 10Member
so, are you saying that i should have a separate variable for each item quantity? then call them individually. i've tried assigning different variable for the different product quantity, and still no joy! an example of what you mean might help me to see a little better.

thanks
hens
• Posts: 395Member
The following is only one way of programming this solution. You could have also done your printing as each subtotal was calculated and used a single variable, but you would have still needed to keep track of the growing total. Study the changes I have made closely. Some, but not all are highlighted in red.
[code]
#include
#include

int main () {
double tvPrice = 400.00;
double vcrPrice = 220.00;
double remoteControlPrice = 35.20;
double cdPlayerPrice = 300.00;

double tapeRecorderPrice = 150.00;
const double tax = 0.0825;
int quantitytv = 0, quantityvcr = 0, quantityrem = 0, quantitycd = 0, quantityrec = 0;
double subTotal = 0;
double total = 0, salesTax;

printf("How Many TV's Were Sold? ");
scanf("%d", &quantitytv); //[red]changed quantity[/red]
[red]//[/red] subTotal = quantity * tvPrice;

printf("How Many VCR's Were Sold? ");
scanf("%d", &quantityvcr); //[red]changed quantity[/red]

printf("How Many Remote Controllers Were Sold? ");
scanf("%d", &quantityrem); //[red]changed quantity[/red]
[red]//[/red] total = subTotal + tax;
[red]//[/red] total = subTotal + tax;

printf("How Many CD's Were Sold? ");
scanf("%d", &quantitycd); //[red]changed quantity[/red]

printf("How Many Tape Recorders Were Sold? ");
scanf("%d", &quantityrec); //[red]changed quantity[/red]

subTotal = quantitytv * tvPrice;
subTotal += quantityvcr * vcrPrice;

subTotal += quantityrem * remoteControlPrice;

subTotal += quantitycd * cdPlayerPrice;

subTotal += quantityrec * tapeRecorderPrice;

salesTax = subTotal * tax;

total = subTotal + salesTax;

printf ("%6s%18s%18s%20s
", "QTY", "DESCRIPTION", "UNIT PRICE","TOTAL PRICE");
printf ("---------------------------------------------------------------
");
printf ("%6d%18s%16.2lf%18.2lf
",quantitytv,"TV",tvPrice,quantitytv * tvPrice);
printf ("%6d%18s%16.2lf%18.2lf
",quantityvcr,"VCR",vcrPrice,quantityvcr * vcrPrice);
printf ("%6d%18s%16.2lf%18.2lf
",quantityrem,"REMOTE CTRLR",remoteControlPrice,quantityrem * remoteControlPrice);
printf ("%6d%18s%16.2lf%18.2lf
",quantitycd,"CD PLAYER",cdPlayerPrice,quantitycd * cdPlayerPrice);
printf ("%6d%18s%16.2lf%18.2lf
",quantityrec,"TAPE RECORDER",tapeRecorderPrice,quantityrec * tapeRecorderPrice);

printf(" SUBTOTAL: %10.2lf
", subTotal);
printf(" TAX: %10.2lf
", salesTax);
printf(" TOTAL: %10.2lf
", total);

system("pause");
return 0;
}
[/code]
Take Care,
Ed
• Posts: 10Member
this worked, thanks