calculate saletax price

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gfanning23
    New Member
    • Mar 2016
    • 12

    calculate saletax price

    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:

    Code:
    int main ()
    {
    /* variable definition: */
    float price, salestax, setprice;
    /* Prompt user for price */
    printf("Enter the price in dollars: \n");
    // Input the price
    scanf("$%f", &price);
    /* Prompt user for salestax */
    printf("Enter the state sales tax: \n");
    // Input the salestax
    scanf("%f", &salestax);
    // Calculate the setprice
    setprice = price + (price * salestax);
    // Print the result
    printf("price with Tax is: $%f\n", &price);
    return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    On line 16, replace &price with price.
    By the way, the price with tax is setprice not price.

    Comment

    • gfanning23
      New Member
      • Mar 2016
      • 12

      #3
      Thanks,

      How would I write that line so it outputs the setprice?

      Comment

      • gfanning23
        New Member
        • Mar 2016
        • 12

        #4
        this is what I did.

        Code:
        #include <stdio.h>
        
        int main ()
        {
        /* variable definition: */
        float price, salestax, setprice;
        /* Prompt user for price */
        printf("Enter the price in dollars: \n");
        // Input the price
        scanf("$%f", &price);
        /* Prompt user for salestax */
        printf("Enter the state sales tax: \n");
        // Input the salestax
        scanf("%f", &salestax);
        // Calculate the setprice
        setprice = price + (price * salestax);
        scanf("$%f", &setprice);
        // Print the result
        printf("price with Tax is: $%f\n", setprice);
        return 0;
        }
        my input is this:
        Price: 19.99
        salestax: .06

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

        What am I doing wrong?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          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.

          Comment

          Working...