Small program that has to calculate miles per gallon

This is a discussion on Small program that has to calculate miles per gallon within the C++ Programming forums, part of the General Programming Boards category; Hi, I am developing a program that should calculate the miles per gallon obtained from tankful. After processing all the ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Small program that has to calculate miles per gallon

    Hi,

    I am developing a program that should calculate the miles per gallon obtained from tankful. After processing all the input data, the program should calculate the combined miles per gallon obtained for all tankfuls.

    So, this is what the output should look like -

    Enter the gallons user( -1 to end): 12.8
    Enter the miles driven: 287
    The miles / gallon for this tank was 22.421875

    Enter the gallons user( -1 to end): 10.3
    Enter the miles driven: 200
    The miles / gallon for this tank was 19.417475

    Enter the gallons user( -1 to end): -1

    The overall average miles/gallon was 21.601423

    This is the code Iv'e done so far. It doesn't give the desired result, as it, does like an endless loop after Iv'e entered the gallons and miles.
    Can anyone give me some advice and/or tips please?
    Code:
    #pragma argsused
    #pragma hdrstop
    #include<iomanip>
    #include<iostream>
    #include<conio>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       // declare the variables
       double gallons;      // read in the gallons
       double miles;        // read in the miles
       double totalGallons; // calculate the total gallon input
       double totalMiles;   // calculate the total mile input
       double total;        // calculate the gallons/miles
       double average;      // calculate the average overall
       double counter;
    
       // initialization phase
       total = 0;       // initialise total
       counter = 1;     // initialise loop counter
    
       cout << "Enter the gallons used( -1 to end): " << endl;
       cin >> gallons;
       cout << "Enter the miles driven: " << endl;
       cin >> miles;
    
       average = miles / gallons; // calculate the the current input
    
       cout << average;   // display average
    
       // loop until sentinal value read from user
       while ( gallons != -1 ) {
          total = totalMiles + totalGallons;//add total of gallons to total of miles
          counter = counter + 1;  // increment the counter
    
          // prompt for input and read the next gallons
          cout << "Enter the gallons user( -1 to end): " << endl;
          cout << "Enter the miles driven: " << endl;
       }
    
       // termination phase
       // if user entered at least one gallon...
       if ( counter != 0 ) {
    
          // calculate average of all gallons over miles entered
          average = static_cast< double >( total ) / counter;
    
          // display overall average with six digits of precision
          cout << "Overall average is " << setprecision( 6 )
               << fixed << average << endl;
    
          }
    
          else
             cout << "No gallons or miles were entered" << endl;
    
       getch();
       return 0;
    }

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Code:
       while ( gallons != -1 ) {
          total = totalMiles + totalGallons;//add total of gallons to total of miles
          counter = counter + 1;  // increment the counter
    
          // prompt for input and read the next gallons
          cout << "Enter the gallons user( -1 to end): " << endl;
          cout << "Enter the miles driven: " << endl;
       }
    How do you expect the user to enter the gallons and miles if it never asks for a value?
    Do not make direct eye contact with me.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Code:
    total = totalMiles + totalGallons;//add total of gallons to total of miles
    And of what value is total? The combination of gallons and miles gives you a worthless number. And anyway, you never set totalMiles nor totalGallons any value.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    i looked through it, and changed it a little, and this is what i got.


    Code:
    #pragma argsused
    #pragma hdrstop
    #include<iomanip>
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       // declare the variables
       double gallons;      // read in the gallons
       double miles;        // read in the miles
       double gallons2;
       double miles2;
       double average3;
       double average4;
       double average;      // calculate the average overall
       double average2;
    
    
       cout << "Enter the gallons used( 0 to end): " << endl;
       cin >> gallons;
       cout << "Enter the miles driven: " << endl;
       cin >> miles;
    
       average = miles / gallons; // calculate the the current input
    
       cout << "your average for the trip was " << average << endl;   // display average
    
       // loop until sentinal value read from user
       if ( gallons != 0 )
       {
    
          // prompt for input and read the next gallons
          cout << "Enter the gallons used( 0 to end): " << endl;
          cin >> gallons2;
          cout << "Enter the miles driven: " << endl;
          cin >> miles2;
          average2 = miles2 / gallons2;
          cout << "your average for trip 2 was " << average2 << endl;
       }
             else
          {
             cout << "No gallons or miles were entered" << endl;
    
       return 0;
        }
    
       // termination phase
       // if user entered at least one gallon...
       if ( gallons2 != 0 )
       {
          average3 = average + average2;  // to get the average of the  2
          average4 = average3 / 2;  // to get the average of the 2
    
          // display overall average with six digits of precision
          cout << "Overall average is " << average4;
          cin.get();
        }
          else
        {
             cout << "No gallons or miles were entered" << endl;
    
       return 0;
        }
    }
    sorry for my lack of comenting. but you should be able to figure it out.

    it worked for me, but it didnt stay up to give me the over all average. maybe one of you can see what was wrong.

    (note: sorry if its redundant/ not clean, still a noob)
    Last edited by Aalmaron; 01-06-2004 at 12:36 AM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Where's your loop?

    It would help if you fix your format so the indentation is consistant.

    And if you're going to apologize for not commenting, comment and save the apology
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Plain and simply- as pointed out by Lurk & Aalmaron- you forgot to read in the values for the variables in your loop i.e. cin>>gallons; and the other. Always make sure you can reach the exit conditon you set for a loop.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Registered User Aalmaron's Avatar
    Join Date
    Jan 2004
    Location
    In front of a monitor
    Posts
    48
    oh hey, if you wanted to loop it, among other things that you'd need to change, you shouldnt have the paramiters be the gas.

    Code:
    int counter = 1
    
    while ( counter = 1)
    {
     yadda yadda yadda
    
     counter = counter + 1;
    }
    
    while ( counter = 2)
    {
     yadda yadda yadda
    
     counter = counter + 1;
    }
    then at the end of the loop add

    Code:
    counter = 1
    you could incorperate that into the code and also mess with the way the math works.
    Last edited by Aalmaron; 01-06-2004 at 02:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to Calculate Molar Mass
    By ebrayer in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 04:20 PM
  2. guidence to a program C
    By nurofen in forum C Programming
    Replies: 17
    Last Post: 04-15-2008, 12:50 AM
  3. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21