Incorrect output

I am creating a car simulator program and the output for the the fuel level is correct. But the mileage is incorrect. It is displaying mileage: 1 -- Fuel Level: 15 then mileage: 2 -- Fuel Level 14 and so on. In the directions it says:

**The maximum mileage the odometer can 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.) **

I have gone through my code but I cant seem to find the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//Main.cpp
#include <iostream>  
#include "FuelGauge.h"
#include "Odometer.h"
using namespace std;


int main ()
{
	FuelGauge fuelG(15);
	Odometer odm(0, &fuelG);
	
	
	//While loop and display 
	while (fuelG.getCurrentAmtFuel() > 0)
	{
		odm.incrementCurrentMileage();
		cout << "Mileage: " << odm.getCurrentAmtMiles() << endl;
		cout << "Fuel Level: " << fuelG.getCurrentAmtFuel() << " gallons" << endl;
		fuelG.decrementFuelTank();
	}
	
	return 0;
}


//FuelGauge.h
#ifndef FUELGAUGE_H
#define FUELGAUGE_H
using namespace std;
class FuelGauge
{
	private:
		int currentAmtFuel; //Holds current amount of fuel 
	
	public:
		FuelGauge (int gallons) // Current amount of fuel in gallons 
		{
			currentAmtFuel = gallons;
		}
		
		FuelGauge(); //Constructor 
		
		int getCurrentAmtFuel () //Gets the current amount of fuel and returns it 
		{
			return currentAmtFuel;
		}
		
		void incrementFuelTank() //Increments the fuel tank 
		{
			if (currentAmtFuel < 15)
			{
				currentAmtFuel++;
			}
		}
		
		void decrementFuelTank() //Decrements the fuel tank 
		{
			if (currentAmtFuel > 0)
			{
				currentAmtFuel--;
			}
		}
};

#endif

//Odometer.h
#include "FuelGauge.h"


#ifndef ODOMETER_H
#define ODOMETER_H
class Odometer
{
	private:
		int currentAmtMiles; //Holds current amount of miles 
		FuelGauge *fuelG; //Creates fuelG under FuelGauge
		//FuelGauge *f;
		
	public:
		Odometer(int miles, FuelGauge *f) //odometer function 
		{
			currentAmtMiles = miles;
			fuelG = f;
		}
		
		int getCurrentAmtMiles () //Function to get the amount of miles 
		{
			return currentAmtMiles;
		}
	
		void incrementCurrentMileage() //Increment mileage function 
		{
			if(currentAmtMiles < 999999)
			{
				currentAmtMiles++;
			}
			else 
				currentAmtMiles = 0;
		}
		
		
};

#endif 
Last edited on
> It should decrease the FuelGauge object’s current amount of fuel by 1 gallon
> for every 24 miles traveled.
¿and where are you doing that?


> I have gone through my code but I cant seem to find the problem.
look at your loop in line 15--21
you are going mile by mile, and at each step decrementing the fuel.
Topic archived. No new replies allowed.