dimanche 1 mars 2015

Logic Issue: Classes and Functions


This is my first program using a class, separate implementation and header files, constructors... The very basics of object oriented programming. This is the first time I have attempted an OOP program rather than procedural. With that said, please forgive my format and anything else that may be difficult to understand.


I'm not sure if the issue I am having is due to all the files being separate (and therefore confusing me), or if my mind is just blank. In either case, here is a run-down of the program:


Essentially, it simulates driving in a vehicle. It displays a menu, the user chooses an option from the menu, and it continues until either the vehicle runs out of gas, or until the user chooses to quit.


If the user chooses to drive (options 1 or 2), they are then asked how far they would like to drive. The user should input the number of miles they wish to drive (ex. 31).


If the user chooses to fill the gas tank, they are asked how many gallons they would like to put in the tank (a full tank is 12 gallons - and if they enter more than will fit in the tank, the program fills up the tank and lets the user know how much actually fit in the tank).


If the user chooses to "show the car data," the program will display the total mileage on the odometer, the current fuel level, the average MPG in the city (a constant of 16.5), the average MPG on the highway (a constant of 24.3), and the average MPG.


The average MPG is where I am having issues. Obviously, if the vehicle only travels in the city, the MPG would be 16.5, and if it only travels on the highway, the MPG would be 24.3... But I can't seem to get the average MPG if the user drives both on the highway and in the city.


EXAMPLE-


(menu displays and user chooses to drive in the city for 31 miles)


(menu displays and user then chooses "show car data")


Mileage = 31


Fuel Level = 10.12


Average city MPG = 16.5


Average highway MPG = 24.3


Average MPG = 16.5


All of this data would be correct. The average MPG should be the same as the city MPG because the user only chose to drive in the city.


However, if the user chooses to drive in the city, and then immediately chooses to drive on the highway, the average MPG is incorrect.


EXAMPLE -


(menu displays and user chooses to drive in the city for 100 miles)


(menu displays and user then chooses to drive on the highway for 110 miles)


(menu displays and user then chooses "show car data")


Mileage = 210


Fuel Level = 1.41


Average city MPG = 16.5


Average highway MPG = 24.3


Average MPG = 24.3 (This is incorrect since the user drove in the city AND on the highway)


Here is the header file:



#ifndef CAR_H
#define CAR_H

class Car
{
private:
const int FULLTANK = 12; //Full tank of gas (12 gallons)
const float CITYMPG = 16.5; //Average mpg when driving in the city
const float HWYMPG = 24.3; //Average mpg when driving on the highway
int mileage = 0; //Total milage on odometer
int miles = 0; //Distance user wants to drive
int gallons = 0; //Number of gallons of gas that the user want to fill car up with
double fuelLevel = 0; //Amount of fuel in 12 gallon tank
double avgMPG = 0; //Average mpg (of city and highway)
double milesDriven = 0; //Number of miles driven on specific trip
double gallUsed = 0; //Number of gallons of gas used on specific trip
double getGas = 0; //Number of gallons that can fit in tank in order to fill it up
public:
Car(int m, double f)
{
mileage = m;
fuelLevel = f;
}
void addfuel(int);
bool citydrive(int);
bool highwaydrive(int);
void showcar();
};

#endif


Here is the implementation file:



#include "Car.h"
#include <iostream>
using namespace std;

void Car::addfuel(int gallons)
{
miles = 0;
milesDriven = 0;
getGas = FULLTANK - fuelLevel;

if (getGas == 0)
{
cout << "Your tank is already full!\n";
}
else if (gallons > getGas)
{
fuelLevel += getGas;
cout << "Your car could only fit " << getGas << " gallons in tank. The tank is now full.\n";
}
else
{
fuelLevel += gallons;
cout << "You now have " << (fuelLevel + gallons) << " gallons of gas in your tank!\n";
}

}

bool Car::citydrive(int miles)
{
bool outOfGas = false;

milesDriven = miles;

mileage += milesDriven;
gallUsed = milesDriven / CITYMPG;
fuelLevel -= gallUsed;

if (fuelLevel < 0)
{
outOfGas = true;
cout << "\nYou ran out of gas!\n\n";
showcar();
exit(0);
}

return outOfGas;
}


Here is the main program:



#include <iostream>
#include <iomanip>
#include "Car.h"
using namespace std;

int main()
{
Car chevy(0, 12.0); //Car class object
int choice; //User's choice from menu
int miles; //Number of miles user wants to drive
int gallons; //Number of gallons to put in gas tank

do
{
//Set numeric output formatting
cout << fixed << showpoint << setprecision(2);

//Display menu
cout << "Let's go on a drive! ";
cout << "What would you like to do?\n";
cout << "1. Drive in the city\n";
cout << "2. Drive on the highway\n";
cout << "3. Add fuel\n";
cout << "4. Show car data\n";
cout << "5. Quit\n";
cout << endl;

//Get user input
cin >> choice;
cout << endl;

//Respond to user's selection
if (choice == 1)
{
cout << "How far would you like to drive in the city?\n";
cin >> miles;
chevy.citydrive(miles);
cout << endl;
}
else if (choice == 2)
{
cout << "How far would you like to drive on the highway?\n";
cin >> miles;
chevy.highwaydrive(miles);
cout << endl;
}
else if (choice == 3)
{
cout << "How many gallons of gas would you like to get?\n";
cin >> gallons;
cout << endl;
chevy.addfuel(gallons);
cout << endl;
}
else if (choice == 4)
{
chevy.showcar();
}
else if (choice == 5)
{
exit(0);
}
else
cout << "Invalid choice. Please enter 1, 2, 3, 4, or 5.\n";
} while (choice != 5);

return 0;
}


SO SORRY FOR THE LONG POST!! (kudos to anyone who actually reads this...)




Aucun commentaire:

Enregistrer un commentaire