Below is my code for a class car efficiency program but I have some errors with the get_gas function. if you can help would appreciate it.

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;


class Car
{

        public:
                Car();
                Car(double);
                ~Car();
                void add_gas(double);
                double get_gas(double);
                void drive(double);
        private:

                double fuel_level;
                double fuel_eff;
                double distance;
                double add;

};
Car::Car()
{
}
Car::Car(double z)
Car::Car(double z)
{
         fuel_level = 0;
         fuel_eff = 0;
         distance = 0;
         add = 0;

        fuel_eff = z;
}



void Car::add_gas( double x)
{

        add = x;
}

void Car::drive( double y)

{

        distance = y;


}
double Car::get_gas(double a)
{
        a = fuel_eff * add;
        fuel_level = (a * distance)/fuel_eff;
        return  fuel_level;

}


int main()
{

        cout << " \n *** Car Class Testing Program *** \n " << endl;
        cout << " Enter fuel efficiency in miles / per gallon: ";
        double fe;
        cin >> fe;

        cout << " How many gallon do you want to fill in the tank?: ";
        double gas;
        cin >> gas;

        cout << " How many miles do you want to drive?: ";
        double miles;
        cin >> miles;

        Car my_beemer(fe);

                my_beemer.add_gas(gas);
                my_beemer.drive(miles);
                cout << " You will have  ";
                cout << my_beemer.get_gas();
                cout << " gallons left in the tank after drive. " << endl;

        return 0;

<< moderator edit: added code tags: [code][/code] >>

Recommended Answers

All 4 Replies

You are immediately overwriting the passed parameter in the first line of your get_gas method. Why?

I am not sure what you mean by that?

He's alluding to this:

double Car::get_gas(double a)
{
a = fuel_eff * add;
fuel_level = (a * distance)/fuel_eff;
return  fuel_level;
}

you are changing the value of what you passed in to the function. (this change will only be temporary as 'a' is not a pointer or a reference)..

thanks, for the help. I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.