Jul 7, 2014 at 2:59pm UTC
NOTE. THIS HAS BEEN EDITED FROM THE PREVIOUS VERSION THAT I POSTED!!! Hi all. I am having a problem with this program seeing as it can't run at all. It seems like the computer does not agree with the beginning of my first three functions. Any help would be greatly appreciated by later today. This is only my third program ever and I'm really struggling. Thanks!
/***************************************
***************************************/
#include <iostream>
#include <iomanip>
#define FLEETAVERAGEMPG 25.0
double getGallons ();
double getMiles ();
double getPricePerGallon ();
double calcTripMileage (double miles, double gallons);
double calcTripCost (double pricePerGallon, double gallons);
double calcTripCostPerMile (double tripCost, double miles);
double calcOverallMPG (double totalMiles, double totalGallons);
void showOneTrip (double tripMileage, double tripCost, double tripCostPerMile);
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showMileageComparison (double overallMPG);
using namespace std;
int main ()
{
double gallons;
double miles;
double pricePerGallon;
double tripCost;
double tripMileage;
double tripCostPerMile;
double totalMiles;
double totalGallons;
double totalCost;
double overallMPG;
string choice;
cout << " Fuel Usage Analysis" << endl;
do
{
gallons = getGallons ();
miles = getMiles ();
pricePerGallon = getPricePerGallon ();
tripMileage = calcTripMileage (miles, gallons);
tripCost = calcTripCost (pricePerGallon, gallons);
tripCostPerMile = calcTripCostPerMile (tripCost, miles);
cout << endl;
showOneTrip (tripMileage, tripCost, tripCostPerMile);
cout << endl;
totalMiles +=miles;
totalGallons += gallons;
totalCost += tripCost;
overallMPG = calcOverallMPG (totalMiles, totalGallons);
cout << "\nDo you want to enter another set? ";
cin >> choice;
}
while (choice == "y" || choice == "Y");
showTotals (totalMiles, totalGallons, totalCost, overallMPG);
cout << endl;
showMileageComparison (overallMPG);
return 0;
}
//***************************************************************************
double getGallons ()
{
double gallons;
cout << "\nEnter the number of gallons of fuel used. ";
cin >> gallons;
if (getGallons < 0.0 || getGallons > 40.0)
{
cout << "Out of range. Must be between 0.0 and 40.0. Re-enter: ";
cin >> gallons;
}
return gallons;
}
//***************************************************************************
double getMiles ()
{
double miles;
cout << "Enter the number of miles traveled. ";
cin >> miles;
if (getMiles < 0.0 || getMiles > 800.0)
{
cout << "Out of range. Must be between 0.0 and 800.0. Re-enter: ";
cin >> miles;
}
return miles;
}
//***************************************************************************
double getPricePerGallon ()
{
double pricePerGallon;
cout << "Enter the price per gallon. ";
cin >> pricePerGallon;
if (getPricePerGallon < 2.00 || getPricePerGallon > 7.00)
{
cout << "Out of range. Must be between 2.00 and 7.00. Re-enter: ";
cin >> pricePerGallon;
}
return pricePerGallon;
}
//***************************************************************************
double calcTripMileage (double miles, double gallons)
{
double tripMileage;
if (gallons == 0)
{
tripMileage = 0;
}
else if (gallons > 0)
{
tripMileage = miles / gallons;
}
return tripMileage;
}
//***************************************************************************
double calcTripCost (double pricePerGallon, double gallons)
{
double tripCost;
tripCost = pricePerGallon * gallons;
return tripCost;
}
//***************************************************************************
double calcTripCostPerMile (double tripCost, double miles)
{
double tripCostPerMile;
if (miles == 0)
{
tripCostPerMile = 0;
}
else if (miles > 0)
{
tripCostPerMile = tripCost / miles;
}
return tripCostPerMile;
}
//****************************************************************************
double calcOverallMPG (double totalMiles, double totalGallons)
{
double overallMPG;
if (totalGallons == 0)
{
overallMPG = 0;
}
else if (totalGallons > 0)
{
overallMPG = totalMiles / totalGallons;
}
}
//***************************************************************************
void showOneTrip (double tripMileage, double tripCost, double tripCostPerMile)
{
cout << fixed << setprecision(2) << "Trip Mileage: " << "mpg" << endl;
cout << fixed << setprecision(2) << "Trip Cost : $" << tripCost << endl;
cout << fixed << setprecision(2) << "Trip Cost per Mile: $" << tripCostPerMile << endl;
}
//****************************************************************************
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG)
{
cout << fixed << setprecision(2) << "Total Miles: " << totalMiles << endl;
cout << fixed << setprecision(2) << "Total Gallons: " << totalGallons <<endl;
cout << fixed << setprecision(2) << "Total Cost: $" << totalCost << endl;
cout << fixed << setprecision(2) << "Overall MPG: " << overallMPG;
}
//****************************************************************************
void showMileageComparison (double overallMPG)
{
double mileageComparison;
mileageComparison = overallMPG - FLEETAVERAGEMPG;
if (mileageComparison > 0)
{
cout << "Your vehicle's mileage is greater than the fleet average by " << mileageComparison << " mpg";
}
else if (mileageComparison == 0)
{
cout << "Your vehicle's mileage is equal to the fleet average.";
}
else if (mileageComparison < 0)
{
cout << "Your vehicle's mileage is less than the fleet average by " << mileageComparison << " mpg";
}
}
Last edited on Jul 7, 2014 at 3:53pm UTC
Jul 7, 2014 at 3:31pm UTC
if a function takes no parameters you still have to end with ().
so:
1 2 3 4 5 6 7
double getGallons() // <-- brackets needed here
{
....
}
and similarly for your forward declarations under you hash define.
Also when you call functions as well:
You can't do this:
You need to read it into a double (i.e. your gallons variable).
PLEASE use code tags. it's really difficult to read when you don't.
Last edited on Jul 7, 2014 at 3:35pm UTC
Jul 7, 2014 at 3:53pm UTC
Thank you for your input! I still can't run the program but I understand what you mean.