| | |
never to old
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 3
Reputation:
Solved Threads: 0
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;
}
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;
}
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: 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: 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: 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.
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: If you have further questions, please feel free to ask.
- Stack Overflow
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;int main() { float item_price, sales_tax, total; float sales_tax_rate = .081f;
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;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;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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
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.
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.
Last edited by Coach_Nate; Nov 11th, 2004 at 12:43 am. Reason: noticed more stuff
Behind every great gymnast - is a mentally unstable coach.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Quick maths problem...
- Next Thread: question about array
Views: 1569 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





