Keeps track of miles per gallon.

This program keeps track of miles driven on a tank of gas. The user is able to reset the amount for a trip.

Introduction

This program keeps track of miles driven on a tank of gas. The user is able to reset the amount for a trip.

How to run

Included is the main.cpp file of the program.  Load the file into any C++ complier to build and run the program.  There is also an executable that has already been compiled for the Windows operating system.

/*
* Odometer created by Richie Leahey.
*
* This program keeps track of miles driven on a tank of gas.
* The user is able to reset the amount for a trip.
*/

#include <iostream>
#include <cstdlib>

using namespace std;

class Odometer {
public:
	Odometer();
	double gasUsed;
	double milesDriven;
	double fuelEfficiency;
	double tripGasUsed;
	double tripMilesDriven;
	double tripFuelEfficiency;
	double totalGasUsed;
	double totalMilesDriven;
	double totalFuelEfficiency;

private:
	void Reset();
	void input();
	void output();
	void flush();
	void instructions();
};

void title();
void input(Odometer& theTrip);
void output(Odometer& theTrip);
void flush(Odometer& theTrip);

Odometer::Odometer() {  // Sets the opening values at 0.
	gasUsed = 0;
	milesDriven = 0;
	fuelEfficiency = 0;
	tripGasUsed = 0;
	tripMilesDriven = 0;
	tripFuelEfficiency = 0;
	totalGasUsed = 0;
	totalMilesDriven = 0;
	totalFuelEfficiency = 0;
}

int main(){

	char run_again;
	char choice;

	Odometer distance;
	title();

	do {
		input(distance);
		output(distance);

		choice = ' '; // This while loop resets the trip calculator upon user request.
		while (choice != 'N' && choice != 'n' && choice != 'Y' && choice != 'y')	{
			cout << "Would you like to reset the trip counter? ";
			cout << "(Y or N): ";
			cout << endl;
			cin >> choice;
		}

		if (choice == 'y' || choice == 'Y')	{
			flush(distance);
		}

		cout << endl << endl;  // This loop gives an option to the user to quit the program.
		cout << "Run again? ";
		cout << "(Y or N)";
		cout << endl;
		cin >> run_again;
	} while ((run_again == 'y') || (run_again == 'Y'));
	cout << "<program end>";
	cout << endl << endl;

	system("pause");

	return 0;

}

void title() {  // Prints the title at the beginning of the program.
	cout << "Gas Mileage Calculator";
	cout << endl;
}

void input(Odometer& theTrip) {  // Lets user enter the amount of gas in the tank.
	cout << endl;
	cout << "Enter the amount of gas in your tank in gallons: " << endl;
	cin >> theTrip.gasUsed;
	while (theTrip.gasUsed < 0) {
		cout << "The number needs to be positive.";
		cout << endl;
		cout << "Enter a positive number: " << endl;
		cin >> theTrip.gasUsed;
		cout << endl;
	}
	cout << endl;  // Lets user enter the distance traveled.
	cout << "Enter the miles traveled on your trip: " << endl;
	cin >> theTrip.milesDriven;
	while (theTrip.milesDriven < 0) {
		cout << "The number needs to be positive.";
		cout << endl;
		cout << "Enter a positive number: " << endl;
		cin >> theTrip.milesDriven;
		cout << endl;
	}
	cout << endl;
}

void output(Odometer& theTrip) {  // The calculation of the fuel efficiency.
	theTrip.fuelEfficiency = (theTrip.milesDriven / theTrip.gasUsed);
	theTrip.tripGasUsed = (theTrip.tripGasUsed + theTrip.gasUsed);
	theTrip.tripMilesDriven = (theTrip.tripMilesDriven + theTrip.milesDriven);
	theTrip.tripFuelEfficiency = (theTrip.tripMilesDriven / theTrip.tripGasUsed);
	theTrip.totalGasUsed = (theTrip.totalGasUsed + theTrip.gasUsed);
	theTrip.totalMilesDriven = (theTrip.totalMilesDriven + theTrip.milesDriven);
	theTrip.totalFuelEfficiency = (theTrip.totalMilesDriven / theTrip.tripGasUsed);

	cout << endl << endl;
	cout << "Gas Used: " << (theTrip.gasUsed) << " gallons";
	cout << endl;
	cout << "Distance Traveled: " << (theTrip.milesDriven) << " miles";
	cout << endl;
	cout << "Fuel Efficiency: " << (theTrip.fuelEfficiency) << " mpg";
	cout << endl << endl;
	cout << "Trip Gas Used: " << (theTrip.tripGasUsed) << " gallons";
	cout << endl;
	cout << "Trip Distance Traveled: " << (theTrip.tripMilesDriven) << " miles";
	cout << endl;
	cout << "Trip Fuel Efficiency: " << (theTrip.tripFuelEfficiency) << " mpg";
	cout << endl << endl;
	cout << "Total Gas Used: " << (theTrip.totalGasUsed) << " gallons";
	cout << endl;
	cout << "Total Distance Traveled: " << (theTrip.totalMilesDriven) << " miles";
	cout << endl;
	cout << "Total Fuel Efficiency: " << (theTrip.totalFuelEfficiency) << " mpg";
	cout << endl << endl;

}

void flush(Odometer& theTrip) {  // Resets the trip counter upon user request.
	theTrip.tripGasUsed = (0);
	theTrip.tripMilesDriven = (0);
	theTrip.tripFuelEfficiency = (0);
}