Question:
help with C++ program?
aesa
2011-10-23 19:50:44 UTC
Write a program that asks the user to enter the price of an item and the sales tax rate as a percent. The program should display the total price of the item (price plus sales tax.) After obtaining the input from the user, the program should use the *= operator to calculate the total price of the item using the equivalent of the following formula.
price = price * (1 + sales_tax_rate)

I am stuck on this one. What is wrong with variable and formula?

#include
#include

using namespace std;

int main()
{

cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);


double price, sales_tax, rate;
double total_price;

cout << " Enter the price of the item ";
cin >> price;
cout << endl;



cout << " Enter the sales tax rate as a percent(%) ";
cin >> rate;
cout << endl;

sales_tax=(rate)/100;
price=price*(1+sales_tax);
total_price = sales_tax*price;



cout << " The price of the item is " << price-total_price << endl;
cout << " The sales tax on the item is " << rate << endl;
cout << " The total price of the item is " << total_price << endl;

system("pause");
return 0;
}
Four answers:
Wertle Woo
2011-10-23 20:36:05 UTC
If you want to store the base price, the tax, and the total price for display purposes at the end, you need to change your arithmetic around.



When you get the tax rate from the user, you are right to divide it by 100. Then you need to multiply that rate by the price to get the total amount of tax:

sales_tax=price*(rate/100);



Then you get the total price by adding the tax to the base price;

total_price=price+sales_tax;



However, this does not comply with your instructions to use the *= operator. If you want to use one of the combo assignment operators, you'd need to switch some code around:



// After getting the price and tax rate.

rate/=100;

sales_tax=price*rate;

cout << "The price of the item is " << price << endl;

cout << "The sales tax on the item is " << sales_tax << endl;

price*=(1+rate); // This could also be replaced with price+=sales_tax;

cout << "The total price of the item is " << price << end;



This accomplishes displaying all values as well as using the *= operator.



I've seen you post other C++ questions over the past couple of weeks. Out of curiosity, is this for a C++ class over the Fall semester? If so, did this class start at the beginning of the semester (August/September)? If it did, I'd strongly suggest a different school for programming. To be stuck on something this basic this late into the semester is a painfully slow pace. Your assignment should have been accomplished in your first 2 days of programming, not 2 months.
Pete
2011-10-23 20:46:57 UTC
Please review the following code. I kept it as close to yours as possible. Look at the changes and try to understand how it works.



cout << setprecision(2)

<< setiosflags(ios::fixed)

<< setiosflags(ios::showpoint);





double price, sales_tax, rate;

// CHANGE: Removed total_price



cout << " Enter the price of the item ";

cin >> price;

cout << endl;



cout << " Enter the sales tax rate as a percent(%) ";

cin >> rate;

cout << endl;



rate /= 100; // CHANGE: rate = rate / 100

sales_tax = price * rate; // CHANGE: Amount of sales tax paid is tax rate(as decimal) x price.

price*=(1+rate); // CHANGE: The total amount paid is the price plus the amount of sales tax.



cout << " The price of the item is " << price - sales_tax << endl; // CHANGE: Original price is the price with tax minus the sales tax

cout << " The sales tax on the item is " << sales_tax << endl;

cout << " The total price of the item is " << price << endl;



system("pause");

return 0;
oops
2011-10-23 20:01:55 UTC
price doesn't need to be recalculated, just use the price that the user enters. total_price should be calculated with the formula that you are using for recalculating price:



    total_price = price * (1 + sales_tax);



And when you display the price, why are you subtracting total_price from price? That doesn't make any sense, just print the price.



    cout << " The price of the item is " << price << endl;
modulo_function
2011-10-23 20:03:13 UTC
Your arithmetic is wrong.



You want

sales_tax = price*rate/100.0;

total_price = price*(1.0+sales_tax);



or

sales_tax ...same...

total_price = price + sales_tax



Do the algebra to demonstrate that those are the same.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...