Thread: Need help with a program that determines the miles per gallon for 3 tanks of gas

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    Need help with a program that determines the miles per gallon for 3 tanks of gas

    This seems to be a popular assignment. I'm most finished with it but I need to average the three readings and I'm not able to get the math to roll the right way.

    Was hoping to get some help on how to finish this thing up.

    Code:
    #include <stdio.h>
    int main (void)
    {
        int input;
        int x;
        int counter = 0;
        float gallonsUsed, milesDriven, mpg;
        float total = 0;
        float avgMPG = 0;
        
        printf ("Welcome to the mileage calculator.\n");
        
        printf ("\n");
    
        printf ("This program will calculate the miles per gallon for you for three tanks of     gas after you have entered the gallons used and miles driven.\n");
        
        printf ("\n");    
    
        
        for ( x = 0; x < 3; x++ ) 
        {
            
            total = total + mpg;
            counter = counter + 1;
        
            printf ("Enter the number of gallons used for tank #%d: ", x + 1);
            scanf ("%f", &gallonsUsed);
            printf ("Enter the number of miles driven: ");
            scanf ("%f", &milesDriven);
        
            mpg = milesDriven / gallonsUsed; //mpg
            printf ("*** The miles per gallon for this tank is %.1f\n", mpg);
            getchar( );
    
            printf ("\n");    
    
        }
    
        avgMPG = (float) total / counter;
        printf ("Your overall average miles per gallon for three tanks is %.1f\n", avgMPG);
    
        printf ("\n");    
        
        printf ("Thank you for using the program. Goodbye.");
    
        printf ("\n");    
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    On line 23 how can you calculate total at this stage? What are the values of total and mpg?

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    2
    I do admit that was me throwing something against the wall and it's wrong. The program still runs, but its not correct.

    I'm totally new to this (as you can probably tell).

    My teachers notes told us we need to do an accumulation of all three and get the average. I'm just not sure what that code is and how to work it in here.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Cause and effect I have decided that it is a topic no longer covered in schools.

    You can not expect; A = B + C with give you a new value after you change B and C.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-28-2011, 12:32 PM
  2. A program that determines odd numbers
    By metros in forum C Programming
    Replies: 2
    Last Post: 04-21-2010, 03:10 PM
  3. C++ Miles Program
    By slimdime in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2008, 04:51 PM
  4. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  5. miles per gallon program
    By JamesCole in forum C Programming
    Replies: 4
    Last Post: 10-02-2002, 10:11 AM

Tags for this Thread