Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include #include \"FuelGauge.h\" #include \"Odometer.h\" using namespace std; i

ID: 3820563 • Letter: #

Question

#include
#include "FuelGauge.h"
#include "Odometer.h"
using namespace std;

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

   for(int i = 0; i < 15; i++)
{
fuel.incrementFuelTank();
}

   while (fuel.getCurrentAmountOfFuel() > 0)
   {
       odm.incrementcurrentMileage();
       cout << "Mileage: " << odm.getCurrentMileage() << endl;
       cout << "Fuel level:" << fuel.getCurrentAmountOfFuel() << " gallons" << endl;
   }

   return 0;
}

// FuelGauge.h
using namespace std;

#ifndef FUELGAUGE_H
#define FUELGAUGE_H

class FuelGauge
{
private:
   int currentAmountOfFuel;

public:
   FuelGauge(int gallons)
   {
       currentAmountOfFuel = gallons;
   }
   FuelGauge();
   int getCurrentAmountOfFuel()
   {
       return currentAmountOfFuel;
   }
   void incrementFuelTank()
   {
       if(currentAmountOfFuel < 15)
{
currentAmountOfFuel++;
}
   }
   void decrementFuelTank()
   {
       if(currentAmountOfFuel > 0)
{
           currentAmountOfFuel--;
}
   }

};


#endif

// Odometer.h
#include "FuelGauge.h"
using namespace std;

#ifndef ODOMETER_H
#define ODOMTER_H

class Odometer
{
private:
   int currentMileage;
   FuelGauge *fuelG;

public:
   Odometer(int miles, FuelGauge *f)
   {
       currentMileage = miles;
       fuelG = f;
   }

   int getCurrentMileage()
   {
       return currentMileage;
   }
   void incrementcurrentMileage()
   {
       if(currentMileage < 999999)
{
currentMileage++;
}
       if(currentMileage == 999999)
       {
       currentMileage = 0;
       }
   }
   void decrementcurrentMileage()
   {
       if(currentMileage > 24)
       {
       currentMileage--;
       }
   }
};

So, I have this code which is supposed to simulate a car odometer and fuel gauge and display the mileage and number of gallons but I just get an endless loop that says "Current amount of mileage is 15". I dont know what is happening, been looking at it for hours

Explanation / Answer

I will tell you the issue but as you have not described desired behaviour I have experimented something which should solve your problem or give you a way to solve it your self.

Added in bold within code.

#include <iostream>

#include "FuelGauge.h"
#include "Odometer.h"
using namespace std;
int main()
{
FuelGauge fuel(15); // We are initialising fuel to 15
Odometer odm(0, &fuel);
for(int i = 0; i < 15; i++)
{
fuel.incrementFuelTank(); // this will not have any effect on fuel in tank as we are incrementing fuel tank only if it is less than 15 but we have initialised it to be 15 in aove statement.
}
while (fuel.getCurrentAmountOfFuel() > 0) // This will loop until fuel is 0 and in your code you are neever decrementing fuel so I have added a statement to decreent fuel
{
odm.incrementcurrentMileage();
cout << "Mileage: " << odm.getCurrentMileage() << endl;
cout << "Fuel level:" << fuel.getCurrentAmountOfFuel() << " gallons" << endl;
fuel.decrementFuelTank();
}
return 0;
}

Also your Odometer is not using fuel reference it get while creating its object.

Please leave a positive rating if this explained problem you were facing.