By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Stuck on C++ program to calculate price and tax

P: 63
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due.

I have to use the formulas:
total cost = number of items * cost-per-item
total cost (discounted) = total cost - (discount rate * total cost)
tax due = total cost * TAXRATE
amount due = total cost + tax due

I am going to put what I have so far. Any suggestions would be helpful. Also, how do I put the discount rate as a percentage?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.     const float TAXRATE = 0.06f;
  9.     double costPerItem, itemsPurchased, discountRate, totalCost, taxDue, totalCostDiscounted;
  10.  
  11.     cout <<"\nEnter the cost-per-item: ";
  12.     cin >> costPerItem;
  13.     cout <<"\nEnter the number of items purchased: ";
  14.     cin >> itemsPurchased;
  15.     cout <<"\nEnter the discount rate: ";
  16.     cin >> discountRate;
  17.  
  18.     totalCost = itemsPurchased * costPerItem;
  19.     totalCostDiscounted = totalCost - (discountRate * totalCost);
  20.     taxDue = totalCost * TAXRATE;
  21.     cout << setiosflags (ios::fixed)
  22.          << setiosflags (ios::showpoint)
  23.          << setprecision(2);
  24.  
  25.     cout <<"\nThe total cost is: " << setw(4) << costPerItem*itemsPurchased  << endl;
  26.     cout <<"\nThe tax due is: " <<setw(4)<<costPerItem*itemsPurchased*TAXRATE << endl;
  27.     cout <<"\nThe amount due is: "<<setw(4)<<totalCost + taxDue<<endl;
  28.  
  29.     return 0;
  30. }
Sep 7 '07 #1
Share this Question
Share on Google+
3 Replies


Savage
Expert 100+
P: 1,764
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due.

I have to use the formulas:
total cost = number of items * cost-per-item
total cost (discounted) = total cost - (discount rate * total cost)
tax due = total cost * TAXRATE
amount due = total cost + tax due

I am going to put what I have so far. Any suggestions would be helpful. Also, how do I put the discount rate as a percentage?


#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
const float TAXRATE = 0.06f;
double costPerItem, itemsPurchased, discountRate, totalCost, taxDue, totalCostDiscounted;

cout <<"\nEnter the cost-per-item: ";
cin >> costPerItem;
cout <<"\nEnter the number of items purchased: ";
cin >> itemsPurchased;
cout <<"\nEnter the discount rate: ";
cin >> discountRate;

totalCost = itemsPurchased * costPerItem;
totalCostDiscounted = totalCost - (discountRate * totalCost);
taxDue = totalCost * TAXRATE;
cout << setiosflags (ios::fixed)
<< setiosflags (ios::showpoint)
<< setprecision(2);

cout <<"\nThe total cost is: " << setw(4) << costPerItem*itemsPurchased << endl;
cout <<"\nThe tax due is: " <<setw(4)<<costPerItem*itemsPurchased*TAXRATE << endl;
cout <<"\nThe amount due is: "<<setw(4)<<totalCost + taxDue<<endl;

return 0;
}
Suggestions you say,hmm.How about couting totalCost and taxDue instead of:

Expand|Select|Wrap|Line Numbers
  1.     cout <<"\nThe total cost is: " << setw(4) << costPerItem*itemsPurchased  << endl;
  2.     cout <<"\nThe tax due is: " <<setw(4)<<costPerItem*itemsPurchased*TAXRATE << endl;
??

Also please use code tags for your code.

Oh,and one more question:

" Also, how do I put the discount rate as a percentage?"

Do you mean output it as percantage,input it as percantage or both?

Savage
Sep 7 '07 #2

P: 63
I am not really sure what a code tag is? I also figured out the percent question. Thanks for the help.
Sep 11 '07 #3

Savage
Expert 100+
P: 1,764
I am not really sure what a code tag is? I also figured out the percent question. Thanks for the help.
You see # button in a message editor,well those arr code tags.

Savage
Sep 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.