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; Hello, I have this project due today for class and I cant seem to get it compiled without errors, it ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    6

    C++ Program Help (MPG calculator)

    Hello, I have this project due today for class and I cant seem to get it compiled without errors, it gives me one error and its not specific enough for me to fix it on my own, so I am asking for help here. Here is the code, and what its "supposed" to do.

    "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."

    And here is my code;

    Code:
    //Assignment 1
    //Gas
    //september-27-05
    //
    
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    int liters;
    double distance;
    double gallon = (0.264179*liters);
    
    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 >0);
    
    return 0;
    
    }
    Thanks in advance to anyone who can help.

    -Brent

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    double gallon = (0.264179*liters);
    You can't do that since liters is undefined at the point of usage. Basically what you want is have it as a constant ratio and then multiply it (factor-label) in the MPG equation:

    Code:
    const double GALLONS_PER_LITER = 0.264179;
    ...
    double mpg = distance / (liters * GALLONS_PER_LITER);

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    int liters = 0;

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    and what speedy said.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    Thanks speed and fooddude, i changed the int liters to equal 0, then i changed out the 2 lines that speedy gave me, and i seem to have more errors, i may have took what speedy said wrong and did the wrong things, this is my code as of now ;

    Code:
    //Gas
    //september-27-05
    //
    
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    int liters = 0;
    double mpg = distance / (liters * GALLONS_PER_LITER);
    const double GALLONS_PER_LITER = 0.264179;
    
    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;
    
    
    return 0;
    and the errors now are;
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2065: 'GALLONS_PER_LITER' : undeclared identifier
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2563: mismatch in formal parameter list
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(23) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(25) : error C2563: mismatch in formal parameter list
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(31) : fatal error C1004: unexpected end of file found

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Pay attention to the errors.

    You use GALLONS_PER_LITER in a calculation before you define it.

    You never delare distance. (and, you probably don't want to use it in a calcualtion until after it's been input by the user)

    Your do while loop has no closing brace... or a while()
    Last edited by spydoor; 09-27-2005 at 04:02 PM.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    Im still confused about the gallons_per_liter error... I fixed everything else and now im only recieving 2 errors about the gallon_per_liter part.. heres my code and errors, if anyone knows a solution (explained in dummy terms for a C++ noob) it would be awesome and greatly appreciated.

    Code:
    /Assignment 1
    //Gas
    //september-27-05
    //frfrf
    
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
    int liters = 0;
    double mpg = distance / (liters*GALLONS_PER_LITER);
    const double GALLONS_PER_LITER = 0.264179;
    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;
    
    }

    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(14) : error C2065: 'GALLONSPERLITER' : undeclared identifier
    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(14) : error C2563: mismatch in formal parameter list

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Look here, these two lines are in the wrong order. You use GALLONS_PER_LITER and then you declare it:
    Code:
    double mpg = distance / (liters*GALLONS_PER_LITER);
    const double GALLONS_PER_LITER = 0.264179;
    Put them the other way around.
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    distance is also in the wrong place. And this line is a syntax error:
    Code:
    /Assignment 1
    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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The calculation of mpg should be moved much farther down. You must wait until the user enters the liters and the distance before you calculate the mpg. Follow the program flow from top to bottom on paper or in your head. You will see that it doesn't make sense as written.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yeah, right now it always calulates 0 litres.
    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.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    6
    how would i move it down? i thought ints and const and doubles ect need to be determined at the top of the code?

    i switched the 2 lines around and now only 1 error...

    C:\Documents and Settings\classics\Desktop\mpgproject.cpp(15) : error C2563: mismatch in formal parameter list

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    i thought ints and const and doubles ect need to be determined at the top of the code?
    In C, they do. In C++, they don't. But you want to declare the variables at the top of the function, and preform the calculation[s] on them later on.
    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.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> i thought ints and const and doubles ect need to be determined at the top of the code?

    Nope, that is not true. There is a style of programming that declares these things at the top of code, but it is not appropriate for C++ anyway. If your instructor is telling you to do that, you can still assign a value later. Initialize mpg to 0.0, and later assign it when you know the values of liters and distance.

    Post your new code to get help with the new error.

    >> you want to declare the variables at the top of the function, and preform the calculation[s] on them later on

    This is not true for C++. Declare your variables only when and where you need them, and initialize them fully when they are declared.
    Last edited by Daved; 09-27-2005 at 05:31 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can do this, you know:
    Code:
    int main() {
        int x, y = 3;
    
        x = y * y;
    }
    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.

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

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