Car Instrument Simulator

I'm also working on this problem:

For this assignment you will design a set of classes that work together to simulate a car's fuel gauge and
odometer. The classes you will design are:
· The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are
- To know the car's current amount of fuel, in gallons.
3- To report the car's current amount of fuel, in gallons.
- To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car.
(The car can hold a maximum of 15 gallons.)
- To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than
0 gallons. This simulates burning fuel as the car runs.
· The Odometer Class: This class will simulate the car's odometer. Its responsibilities are:
- To know the car's current milage.
- To report the car's current milage.
- To be able increment the current mileage by 1 mile. The maximum mileage the odometer
can be store is 999, 999 miles. When this amount is exceeded, the odometer resets the current
mileage to 0.
- To be able to work with a FuelGauge object. It should decrease the FuelGauge object's
current amount of fuel by 1 gallon for every 24 miles traveled. (The car's fuel economy is 24
miles per gallon).
Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then
run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print
the car's current milage and amount of fuel.

Here is the program I have so far:

# include <iostream>
# include <string>
using namespace std;

class FuelGauge
{
private:
int CurrentFuel;
public:
FuelGauge();
~FuelGauge();
FuelGauge(int g)
{
CurrentFuel = g;
}



int getCurrentFuel()
{
return CurrentFuel;
}

void IncrementFuel()
{
if (CurrentFuel < 15)
CurrentFuel++;
}

void DecrementFuel()
{
if (CurrentFuel > 0)
CurrentFuel--;
}

};

FuelGauge::FuelGauge()
{

}
FuelGauge::~FuelGauge()
{

}

class Odometer
{
private:
int CurrentMileage;
FuelGauge *fuel;
public:
Odometer();
~Odometer();
Odometer(int miles, FuelGauge *f)
{
CurrentMileage = miles;
fuel = f;
}

int getCurrentMileage()
{
return CurrentMileage;
}
void incrementCurrentMileage()
{
if (CurrentMileage < 999999)
CurrentMileage++;
else
CurrentMileage = 0;
}

void decrementCurrentMileage()
{
if (CurrentMileage > 24)
CurrentMileage--;
(*fuel).DecrementFuel();
}

};

Odometer::Odometer()
{

}
Odometer::~Odometer()
{

}

int main()
{
FuelGauge fuel(15);
Odometer odo(0, &fuel);

while (fuel.getCurrentFuel() > 0)
{
odo.incrementCurrentMileage();
cout << " Milage: " << odo.getCurrentMileage << " The fuel level: " << fuel.getCurrentFuel << " gallons " << endl;
}

system("pause");
return 0;
}

And I am getting these Errors:

Error 2 error C3867: 'FuelGauge::getCurrentFuel': function call missing argument list; use '&FuelGauge::getCurrentFuel' to create a pointer to member

Error 1 error C3867: 'Odometer::getCurrentMileage': function call missing argument list; use '&Odometer::getCurrentMileage' to create a pointer to member

I don't know if I should change up my program if it is asking for the "&" after the functions or No clue where i should place the & if its going to change other parts of my program.

why are complicating things by using pointers? I don't see that requirement?

anyway. your errors are because you've not called the functions correctly. you've forgotten to add the brackets at the end:
 
cout << " Milage: " << odo.getCurrentMileage() << " The fuel level: " << fuel.getCurrentFuel() << " gallons " << endl;


Last edited on
Think I was just adding some things to see if it could fix the problem But I changed it and it works but the output doesn't stop its going in a continuous loop
that's because you're looping it 999999 times which will take ages, and then even when you hit that it'll reset to zero and start again. So yea. It will loop continuously :)

EDIT: you only call DecrementFuel() in decrementCurrentMileage(), and you NEVER call this function. So in other words this:
while (fuel.getCurrentFuel() > 0)
will always be true.

edit 2: this logic is wrong:
1
2
if (CurrentMileage > 24)
			CurrentMileage--;
Last edited on
I still need help on this problem. How can I stop the infinite loop? and even if I do I'm not sure if the program would even be working correctly
Topic archived. No new replies allowed.