Simple input-output, keeps returning $0.00000

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adalta
    New Member
    • Jan 2015
    • 1

    #1

    Simple input-output, keeps returning $0.00000

    I wrote a simple code to calculate the total cost of gas for a week.

    #include <stdio.h>

    int main () {

    #define MILES_PER_GALLO N 23;
    #define MILES_TO_SCHOOL 25;

    double cost_per_gallon ;
    double trips;

    printf("How much does gas cost this week?\n");
    scanf("%lf", &cost_per_gallo n);

    printf("How many times does John have to drive to campus this week?\n");
    scanf("%lf", &trips);
    trips = trips * 2;

    double miles = MILES_TO_SCHOOL * trips;
    double gallons = miles / MILES_PER_GALLO N
    double cost = cost_per_gallon * gallons;

    printf("John must spend $%lf on gas this week.\n", &cost);
    }


    upon compiling and running it, the final print gives "John must spend $0.00000 on gas this week."

    Why?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    In your printf statement you specify &cost. That would be the address of cost. You want just cost.

    You would use &cost in scanf to place a value in cost.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Also missing semicolon after deceleration of gallons

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        It turns out that the semi-colon is supplied by the macro.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Suggest you remove the semicolons from the two macro definitions. That will require you to add a semicolon to the gallons assignment.

          This has the benefit of removing semicolon from middle of miles assignment.

          Comment

          Working...