# Calculating Sales Tax

Page 1 of 1

## 8 Replies - 31882 Views - Last Post: 31 May 2009 - 10:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=107892&amp;s=ead70a1a4b95eb185c6b65aba38adaf2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ss_hat

Reputation: 0
• Posts: 29
• Joined: 01-March 09

# Calculating Sales Tax

Posted 31 May 2009 - 05:22 PM

Ok, here's the assignment: (issues below code)

Quote

Objective:
At the end of this lab you should know how to:
1. write a call to a value-returning function
2. write a prototype for a value-returning function
3. write a function definition (function heading and body) for a value-returning function

Lab 3A:

Write a program that will prompt the user to enter the amount of a purchase. The program should compute the amount of sales tax and the total purchase price. Declare the sales tax rate as a constant of 0.09 (9 percent) before int main( ).. The program should display the amount of the purchase, the amount of sales tax (not the tax rate) and the total purchase price. Each part of your output should be labeled so that the reader knows what each number represents.

Make sure you include a cout statement at the beginning of your program that says
This program written by: your name

Your program should include two functions. One function should calculate the amount of sales tax and the other function should calculate the total purchase price.

Run your program with 100.00 as the amount of purchase.
Run your program again with 5.50 as the amount of purchase.

-----------------------------------
What I've managed to get done:
```#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;

const double TAX = .09;

double CalcSalesTax(double item1,double TAX)
{
double salesTax;
salesTax = (item1 * TAX);
return salesTax;
}

int main()
{
double item1;
double TAX = .09;
double salesTax;

cout << "Enter the amount of the item purchased:" << endl;
cin >> item1;
salesTax = CalcSalesTax(item1, TAX);
cout << "Taxes on this item are: " << "\$" << salesTax << endl;
cout << "The item purchased was: " << "\$" << item1 << endl;
cout << "Total amount of purchase is: " << "\$" << setprecision(2)<< (salesTax + item1) << endl;
getch();
return 0;
}

```

-----------------

Issue:
I'm trying to get the final output of all three items, ( salesTax, item1 & total amount of purchase) to have the totals print out with only two decimal places. and not 3 like when I put in \$5.50. see below.

--------------------------------------------------------------------------------------------------------
Enter the amount of the item purchased:
5.50
Taxes on this item are: \$0.495
The item purchased was: \$5.5
Total amount of purchase is: \$6

I want the above to print out like this:

Enter the amount of the item purchased:
5.50
Taxes on this item are: \$0.49
The item purchased was: \$5.50
Total amount of purchase is: \$6.00

Is This A Good Question/Topic? 0

## Replies To: Calculating Sales Tax

### #2 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:28 PM

```setprecision(2) << fixed << (salesTax + item1) << endl;

```

Maybe? I'm not really sure.

### #3 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:30 PM

```printf(" %2f", salesTax+item1);
```

This post has been edited by IngeniousHax: 31 May 2009 - 05:35 PM

### #4 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:33 PM

```    cout << "Enter the amount of the item purchased:" << endl;
cin >> item1;
salesTax = CalcSalesTax(item1, TAX);
cout << fixed << setprecision(2);//add this line here
cout << "Taxes on this item are: " << "\$" << salesTax << endl;
```

fixed makes it show a decimal point. setprecision tells how many decimal places to show.

### #5 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:35 PM

IngeniousHax, on 31 May, 2009 - 08:30 PM, said:

```printf(" %2d", salesTax+item1);
```

Not sure he'd want to combine C and C++.

### #6 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:38 PM

apw5020, on 31 May, 2009 - 07:35 PM, said:

Not sure he'd want to combine C and C++.

Right, and you want to keep it as simple as possible. Does that code format all the subsequent lines, or only one? You don't want to have to repeat if you can help it.

### #7 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:48 PM

Touche, I just enjoy the way printf works. It seems to be quite a nice little function. But I suppose if you weren't trying to mic C and C++ the
```#include <iomanip>

int main() {
//whatever code
cout >> fixed >> setprecision(2);

```

Will work just as good.

EDIT:: My >> should be <<...Sorry, should pay more attention.

This post has been edited by IngeniousHax: 31 May 2009 - 06:17 PM

### #8 ss_hat

Reputation: 0
• Posts: 29
• Joined: 01-March 09

## Re: Calculating Sales Tax

Posted 31 May 2009 - 05:51 PM

@OliveOyl3471

Your addition worked perfectly. I'm in C++ first year and we are only in the 3rd chapter of D.S.Malik's book ( http://is.gd/L0Bu ) and that was exactly what I was looking for.

### #9 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Calculating Sales Tax

Posted 31 May 2009 - 10:52 PM

I am glad that I could help.