I am trying to start the odometer reading at 0 to start with, but I want it to increase everytime the user inputs MilesDriven. I can't seem to get it to work...any ideas?

//Paul Baker
//TRCC Intermediate C++
//Assignment 3,  Odometer


#include <iostream>
using namespace std;

class Odometer
{
public:
	void OdometerReset();
	void MPG();
	int MilesDriven;
	int MilesPerGallon;
	int OdometerReading;
};

int main()
{

	while (true)
	{
		Odometer TodaysMiles;
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}
	
}

void Odometer::MPG()
{
	double TotalMPG;


	MilesPerGallon = 200;

	TotalMPG = MilesPerGallon / (MilesDriven * 1.0);
	OdometerReading += MilesDriven;

	cout << "Miles Per Gallon is: " << MilesPerGallon << endl;
	cout << "Fuel Efficiency is: " << TotalMPG << endl;
	cout << "Odometer Reading is: " << OdometerReading << endl;

}

Recommended Answers

All 9 Replies

Your 'function-local' variable named TotalMPG we be created and thusly destroyed with every call of the MPG() function.

Try creating a class variable that will retain the odometer reading.

Your 'function-local' variable named TotalMPG we be created and thusly destroyed with every call of the MPG() function.

Try creating a class variable that will retain the odometer reading.

I see what you're saying about that variable, but I think that's fine to stay the way it is, because that will be established each time the user inputs the miles (i.e. the efficiency will change for each trip). The problem I'm trying to figure out is why my OdomterReading variable, which is a class variable I think, has an jumbled mess for an output. Basically I want it to start at zero, and then increase by the MilesDriven input from the user. What I can do is:

void Odometer::MPG()
{
	double TotalMPG;


	MilesPerGallon = 32;

        OdometerReading = 0;
	TotalMPG = (MilesDriven * 1.0) / MilesPerGallon;
	OdometerReading += MilesDriven;

	cout << "Miles Per Gallon is: " << MilesPerGallon << endl;
	cout << "Fuel Efficiency is: " << TotalMPG << "%" << endl;
	cout << "Odometer Reading is: " << OdometerReading << endl;

}

but that resets the odometer to zero at the end of the while loop. I don't know how to keep the input stored into OdometerReading, each and every time, so that the miles can be added each time.

I see what you're saying about that variable, but I think that's fine to stay the way it is, because that will be established each time the user inputs the miles (i.e. the efficiency will change for each trip). The problem I'm trying to figure out is why my OdomterReading variable, which is a class variable I think, has an jumbled mess for an output. Basically I want it to start at zero, and then increase by the MilesDriven input from the user. What I can do is:

void Odometer::MPG()
{
	double TotalMPG;


	MilesPerGallon = 32;

        OdometerReading = 0;
	TotalMPG = (MilesDriven * 1.0) / MilesPerGallon;
	OdometerReading += MilesDriven;

	cout << "Miles Per Gallon is: " << MilesPerGallon << endl;
	cout << "Fuel Efficiency is: " << TotalMPG << "%" << endl;
	cout << "Odometer Reading is: " << OdometerReading << endl;

}

but that resets the odometer to zero at the end of the while loop. I don't know how to keep the input stored into OdometerReading, each and every time, so that the miles can be added each time.

you have to put the object (TodaysMiles) outside of the while loop, then it will not be recreated every loop

int main()
{
    Odometer TodaysMiles;

	while (true)
	{
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}

}

you have to put the object (TodaysMiles) outside of the while loop, then it will not be recreated every loop

int main()
{
    Odometer TodaysMiles;

	while (true)
	{
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}

}

Here is the output that I'm getting though:

Please enter Miles Driven: 1000

Miles Per Gallon is: 32
Fuel Consumed is: 31.25
Odometer Reading is: -858992460

Please enter Miles Driven:

With the following code:

#include <iostream>
using namespace std;

class Odometer
{
public:
	void OdometerReset();
	void MPG();
	int MilesDriven;
	int MilesPerGallon;
	int OdometerReading;
};

int main()
{
	Odometer TodaysMiles;

	while (true)
	{
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}
	
}

void Odometer::MPG()
{
	double TotalMPG;


	MilesPerGallon = 32;

	TotalMPG = (MilesDriven * 1.0) / MilesPerGallon;
	OdometerReading += MilesDriven;

	cout << "Miles Per Gallon is: " << MilesPerGallon << endl;
	cout << "Fuel Consumed is: " << TotalMPG << endl;
	cout << "Odometer Reading is: " << OdometerReading << endl;

}

I am trying to start the odometer reading at 0 to start with, but I want it to increase everytime the user inputs MilesDriven. I can't seem to get it to work...any ideas?

//Paul Baker
//TRCC Intermediate C++
//Assignment 3,  Odometer


#include <iostream>
using namespace std;

class Odometer
{
public:
	void OdometerReset();
	void MPG();
	int MilesDriven;
	int MilesPerGallon;
	int OdometerReading;
};

int main()
{

	while (true)
	{
		Odometer TodaysMiles;
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}
	
}

void Odometer::MPG()
{
	double TotalMPG;


	MilesPerGallon = 200;

	TotalMPG = MilesPerGallon / (MilesDriven * 1.0);
	OdometerReading += MilesDriven;

	cout << "Miles Per Gallon is: " << MilesPerGallon << endl;
	cout << "Fuel Efficiency is: " << TotalMPG << endl;
	cout << "Odometer Reading is: " << OdometerReading << endl;

}

ok, you can initialise the variable(OdometerReading) as soon as you create the object, or implement a constructor.

int main()
{
    Odometer TodaysMiles;
    TodaysMiles.OdometerReading = 0;

	while (true)
	{
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}
}

You could try to do this...

OdometerReading = OdometerReading + MilesDriven;

You could try to do this...

OdometerReading = OdometerReading + MilesDriven;

it's exactly the same that he is doing.

OdometerReading += MilesDriven;

I am trying to start the odometer reading at 0 to start with, but I want it to increase everytime the user inputs MilesDriven. I can't seem to get it to work...any ideas?

1st Do you know what a constructor is

in you class
add in the public Odometer(); there is no return

now this function gets called when you create
an Odometer now you can
set an intial value in this function
so you can make sure that you have set a value to 0
by calling Odometer_reset you can then return the value to 0

a function needs to be written like has been done for MPG()

ok, you can initialise the variable(OdometerReading) as soon as you create the object, or implement a constructor.

int main()
{
    Odometer TodaysMiles;
    TodaysMiles.OdometerReading = 0;

	while (true)
	{
		cout << "Please enter Miles Driven: ";
		cin >> TodaysMiles.MilesDriven;
		cout << endl;

		TodaysMiles.MPG();

		cout << endl;
	}
}

Thanks! That solved it!

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.