C++ Classes

Why getting the gallons left in my code result in the same value as adding gallons? The final line that should be output after running this ought to be the fuel after subtracting (miles driven / efficiency in miles per gallon). So it should be less than 32, but it isn't.

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
#include <iostream>

using namespace std;

class Car
{
	public:
		Car(int inputEff);
		void drive(int inputMi);
		void addGas(int inputAmt);
		int getGasLevel();
	private:
		int efficiency, fuel = 0;
		
};

Car::Car(int inputEff)
{
	efficiency = inputEff;
}

void Car::drive(int inputMi)
{
	int gallonsUsed = inputMi/efficiency;
	fuel -+ gallonsUsed;
}

int Car::getGasLevel()
{
	return fuel;
}

void Car::addGas(int inputAmt)
{
	fuel += inputAmt;
}

int main()
{
	// 42 miles per gallon red truck
	Car redTruck(42);
	cout << "42 mpg truck" << endl;

	// add 32 gallons of gas (it's a huge tank)
	redTruck.addGas(32);
	cout << "truck now has 32 gallons" << endl;

	// drive 93 miles
	redTruck.drive(93);
	cout << "drove 93 miles" << endl;

	cout << "gallons left: " << redTruck.getGasLevel() << endl;
	return 0;
}
Nevermind, I'm an idiot (L25 -> fuel -= gallonsUsed;)
On line 25 you wrote fuel -+ gallonsUsed but meant fuel -= gallonsUsed. Such typos are really easy to overlook without help.

If you enable compiler warnings, your compiler can help you avoid many similar mistakes in the future. For example, after turning warnings on, GCC reports
main.cpp:25:7: warning: statement has no effect [-Wunused-value]
Which points right to line 25.

By the way, a -+ b is treated as a - (+b).
Last edited on
<Edited to not just repeat what mbozzi said>

Also note that all your arithmetic is integer-based. Maybe this is not a concern for this assignment, but if your 'efficiency' is 42 and you drive 93 miles, 93/42 in integer arithmetic is 2 gallons, not 2.21 gallons. To store partial gallons, you'd need to use floating-point numbers (double).
Last edited on
Thank you mbozzi and Ganado, but should I use double or float in this case?
Use double; it is the default C++ floating point type.
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
#include <iostream>
#include <iomanip>

using namespace std;

class Car
{
public:
	Car(double inputEff);
	void drive(double inputMi);
	void addGas(double inputAmt);
	double getGasLevel() const;
private:
	double efficiency {}, fuel {};

};

Car::Car(double inputEff) : efficiency(inputEff) {}

void Car::drive(double inputMi)
{
	fuel -= inputMi / efficiency;
}

double Car::getGasLevel() const
{
	return fuel;
}

void Car::addGas(double inputAmt)
{
	fuel += inputAmt;
}

int main()
{
	// 42 miles per gallon red truck
	Car redTruck(42);
	cout << "42 mpg truck" << endl;

	// add 32 gallons of gas (it's a huge tank)
	redTruck.addGas(32);
	cout << "truck now has 32 gallons" << endl;

	// drive 93 miles
	redTruck.drive(93);

	cout << "drove 93 miles" << endl;
	cout << "gallons left: " << fixed << setprecision(2) << redTruck.getGasLevel() << endl;
	return 0;
}


Note the constructor using a member initialization list. This is the preferred way to initialise member variables.
Topic archived. No new replies allowed.