Calculating Sales Tax

setting the decimal place to 2 issues

Page 1 of 1

8 Replies - 5842 Views - Last Post: 31 May 2009 - 10:52 PM Rate Topic: -----

#1 ss_hat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 apw5020  Icon User is offline

  • D.I.C Addict

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

Re: Calculating Sales Tax

Posted 31 May 2009 - 05:35 PM

View PostIngeniousHax, on 31 May, 2009 - 08:30 PM, said:

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


Not sure he'd want to combine C and C++.
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Calculating Sales Tax

Posted 31 May 2009 - 05:38 PM

View Postapw5020, 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. :)
Was This Post Helpful? 0
  • +
  • -

#7 IngeniousHax  Icon User is offline

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

Reputation: 78
  • View blog
  • Posts: 1,358
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 ss_hat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Calculating Sales Tax

Posted 31 May 2009 - 10:52 PM

:D I am glad that I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1