I'm told you are never to old to learn something new but right now I don't believe that. I thought I understood this but the program I wrote just doesn't work any suggestions?

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 th 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)`

``````This program calculates Total price of the item (price+sales tax).

#include <iostream.h>
#include<iomanip.h>

//using namespace std;
int main()

{
const price*=sales_tax_rate,
price of item,
sales_tax,
total;
//Display price of item
cout<<"\n Enter the price of the item;
cin>> item_price

//calculate tax
sales_tax=item price*sales_tax_rate;
total= item_price + sales_tax;

//Display price and total
cout<<"\n price of item" <<item_price;
cout<<"\nsales tax" <<sales_tax;
cout<<"\n total amount<<total;

return 0;

}
``````

Edited by happygeek: fixed formatting

Use code tags.
3
Contributors
2
Replies
3
Views
13 Years
Discussion Span
Last Post by Coach_Nate

Greetings,

There are a few syntatical errors in your program. Let's walk through them one step at a time.

Point A
First issue is the local variables themselves. Nothing major, it can be fixed. Let's look at what we have:

``````const price*=sales_tax_rate,
price of item,
sales_tax,
total;``````

Alright, this may cause a few errors if not a ton. I see you declared price as a constant variable that equals sales_tax_rate, though there is no trace of such a variable here. Of course you are initializing a constant variable for the rest without a data type. Since prices and percentages work with decimals alot, lets make these variables floating-type variables:

``````int main() {
float item_price, sales_tax, total;
float sales_tax_rate = .081f;``````

All four variables are floats. The last variable, sales_tax_rate is 8.1% in decimal style, or 0.81. The f is there to tell the compiler this is a float not a double. That is on an enitrely different topic, so lets move to the next step.

Point B
Secondly we need to input from the user to store in our float item_price. This is easy:

``````//Display price of item
cout << "Enter the price of the item: ";
cin >> item_price;``````

As you may see we used cout and cin. Each ending with a semi-colon to break that lines worth of code. Without that semi-colon, the next line may whine about how a computation that started never ended.

Point C
Here we calculate our tax by multiplying the items price by the tax rate we set as 0.81.

``````//calculate tax
sales_tax = item_price * sales_tax_rate;
total = item_price + sales_tax;``````

This is quite simple. The equation is set to sales_tax, and the total adds it to the price, etc...

Of course we just display it now:

``````//Display price and total
cout << "\nprice of item: " << item_price;
cout << "\nsales tax: " << sales_tax;
cout << "\ntotal amount: " << total;

return 0;
}``````

- Stack Overflow

not to mention that your "using namespace std;" at the top is commented out. Also, relating to stack's comment on syntax, there are a few lines with commas at the end rather than semicolons.

happy coding & don't give up!
And remember, your head stops hurting after you stop beating it off the wall.
ok, seriously, anything worth doing was ever learned overnight.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.