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

calculate saletax price

P: 12
I need to be able to show the output of price in dollars, state tax and price with sales tax, so far I have the following. What am I doing wrong or what have I left out. The code runs but doesn't not show any output.

the code:

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3. /* variable definition: */
  4. float price, salestax, setprice;
  5. /* Prompt user for price */
  6. printf("Enter the price in dollars: \n");
  7. // Input the price
  8. scanf("$%f", &price);
  9. /* Prompt user for salestax */
  10. printf("Enter the state sales tax: \n");
  11. // Input the salestax
  12. scanf("%f", &salestax);
  13. // Calculate the setprice
  14. setprice = price + (price * salestax);
  15. // Print the result
  16. printf("price with Tax is: $%f\n", &price);
  17. return 0;
  18. }
Mar 11 '16 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 2,396
On line 16, replace &price with price.
By the way, the price with tax is setprice not price.
Mar 11 '16 #2

P: 12
Thanks,

How would I write that line so it outputs the setprice?
Mar 11 '16 #3

P: 12
this is what I did.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5. /* variable definition: */
  6. float price, salestax, setprice;
  7. /* Prompt user for price */
  8. printf("Enter the price in dollars: \n");
  9. // Input the price
  10. scanf("$%f", &price);
  11. /* Prompt user for salestax */
  12. printf("Enter the state sales tax: \n");
  13. // Input the salestax
  14. scanf("%f", &salestax);
  15. // Calculate the setprice
  16. setprice = price + (price * salestax);
  17. scanf("$%f", &setprice);
  18. // Print the result
  19. printf("price with Tax is: $%f\n", setprice);
  20. return 0;
  21. }
my input is this:
Price: 19.99
salestax: .06

What I get is:
price with Tax is: $-26.856455

What am I doing wrong?
Mar 11 '16 #4

Expert 100+
P: 2,396
Line 16 calculates setprice, but then line 17 throws away that calculation.

I suspect you may not be setting price properly. I suggest you follow each of the line 10 and 14 scanf's with a printf to confirm the value was entered properly.

Take a look at the definition of scanf. Notice how the return value tells you how many conversions took place. For lines 10 and 14, the return value should be 1. You should verify this and print an error message if the return value is anything else.
Mar 12 '16 #5

Post your reply

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