C++ Program Help (MPG calculator)

This is a discussion on C++ Program Help (MPG calculator) within the C++ Programming forums, part of the General Programming Boards category; sorry i forgot to post it i guess Code: //Gas //september-27-05 //frfrf #include <iostream> using namespace std; int main() { ...

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    sorry i forgot to post it i guess


    Code:
    //Gas
    //september-27-05
    //frfrf
    
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    int liters = 0;
    const double GALLONS_PER_LITER = 0.264179;
    double mpg = distance / (liters*GALLONS_PER_LITER);
    double distance = 0;
    
    do
    {
    cout << "Please input how many liters of gasoline is in your vehicle";
    cin >> liters;
    cout << "Please input the distance in miles you traveled in your vehicle.";
    cin >> distance;
    
    cout << "Your vehicle's MPG is" << (distance/liters) << endl;
    
     }while(liters >-1);
    
    return 0;
    
    }
    ok so say i turn double mpg = distance / (liters*GALLONS_PER_LITER);

    into


    double mpg = 0.0

    then where would i put the distance / (liters*GALLONS_PER_LITER); ?

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    After you have the variables involved, of course.

    You also need to save the result of the calculation. You can't just go
    Code:
    distance / (liters*GALLONS_PER_LITER);
    but rather have to go
    Code:
    mpg = distance / (liters*GALLONS_PER_LITER);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    then where would i put thYou are dividing distance by (liters*GALLONS_PER_LITER);?
    Would it not make sense to do that after you have input the distance? Other wise you are just dividing 0 by something (which will always be zero).

  4. #19
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    think about the flow of your program.

    -Ask two questions.
    -store the answers.
    -Use the answers in a calculation.
    -display the results.

    The value of your variable can be changed as many times as you want throughout your program, so don't get confused with thinking that you have to have the final result at the time of declaration.

    Also, as stated, in C++ you do not have to declare your variables at the top of your code.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
       const double GALLONS_PER_LITER = 0.264179; //give a meaningful name to conversion constant
       
       int liters = 0;         //will be set by cin before we use in calculation, but 
       double distance = 0.0;  //it's nice to get in the habit of initializing variables
       
       double mpg = 0.0; //could (should) wait to declare this until closer to where we need it
    
       do
       {
          cout << "Please input how many liters of gasoline is in your vehicle: ";
          cin >> liters;
          
          cout << "Please input the distance in miles you traveled in your vehicle: ";
          cin >> distance;
          
          //We've got our user input... let's do our calculation.
          mpg = distance / (liters * GALLONS_PER_LITER);
          // OR 
          //we could just as well declare mpg here and omit mpg lines above
          //double mpg = distance / (liters * GALLONS_PER_LITER);
          
          //display results
          cout << "Your vehicle's MPG is: " << mpg << endl;
    
       }while(liters >-1);
    
       return 0;
    }

  5. #20
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    thank you everyone who helped!

    -brent

  6. #21
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by mrwooter
    "A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles traveled by the car, and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defines constant for the number of liters per gallon."
    You need to make sure you read your assignment carefully, he wants a global constant that stores the 0.264179 conversion rate. Global means its defined outside of any function.

    ie:
    Code:
    #include <stdio.h>
    
    const double foo = 0.264179; /* globally defined, accessible from 
                                                        any function */
    
    int main(void)
    {
      /* do things here */
    
      return 0;
    }
    Note: this is a C example obviously, but it shows the concept your teacher is looking for.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2008, 08:36 AM
  2. Need some help with Calculator program
    By Sshakey6791 in forum C++ Programming
    Replies: 0
    Last Post: 12-21-2008, 08:16 AM
  3. Help with calculator program.
    By banjordan in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2003, 09:01 PM
  4. Newbie Needs Help on Calculator Program
    By CompWiz84 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2002, 02:21 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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