Hi. I am working with a constructor for Odometer and gallons used per miles. Whe
ID: 3622321 • Letter: H
Question
Hi. I am working with a constructor for Odometer and gallons used per miles.When I compile it, it works fine, but when it displays the results, the miles totaled are zero and the gallons used is garbage.
Can anyone please take a look and see what I did wrong?
Thank you.
Here is what I have:
//File Name: Ch10_11
//Assignment: Chapter 10 Prj 11
//Description: Determines the amount of gallons used
//for fixed miles of 880 based on 34 miles per gallon
#include <iostream>
#include <cmath>
using namespace std;
//Class for Odometer
class Odometer
{
private:
double miles;
double mpg;
public:
Odometer()
{
miles=0.0;
mpg=0.0;
}
void SetMPG (double fe);
//sets the mpg based on the fuel effiency
void AddMiles(double m);
//addes miles to the current miles
void reset();
//resets the miles and mpg
double getGallons();
//returns gallons
double getmiles();
//returns miles
};
int main()
{
Odometer objODM;
double gal_used(0.0);
char choice;
do
{
objODM.SetMPG(34);
objODM.AddMiles(450);
objODM.AddMiles(95);
objODM.AddMiles(335);
gal_used=objODM.getGallons();
cout << "For " << objODM.getmiles() << " miles ";
cout << gal_used << " gallons were used ";
//asks if user wants to repeat the program
cout << "Do you wish to continue? Y/N ";
cin >> choice;
}
while(choice == 'Y' || choice == 'y');
return 0;
}
void Odometer::SetMPG(double fe)
//sets the mpg based on the fuel effiency
{ if(fe>0 && fe<70)
double mpg=fe;
}
void Odometer::AddMiles(double m)
//addes miles to the current miles
{ if(m>0 && m<1000)
double miles=m;
}
void Odometer::reset()
//resets the miles and mpg
{ double miles=0.0;
double getGallons();
}
double Odometer::getmiles()
{ return miles;
}
double Odometer::getGallons()
{ return miles/mpg;
}
Explanation / Answer
please rate - thanks
you were creating new variables not using those in the class
//File Name: Ch10_11
//Assignment: Chapter 10 Prj 11
//Description: Determines the amount of gallons used
//for fixed miles of 880 based on 34 miles per gallon
#include
#include
using namespace std;
//Class for Odometer
class Odometer
{
private:
double miles;
double mpg;
public:
Odometer()
{
miles=0.0;
mpg=0.0;
}
void SetMPG (double fe);
//sets the mpg based on the fuel effiency
void AddMiles(double m);
//addes miles to the current miles
void reset();
//resets the miles and mpg
double getGallons();
//returns gallons
double getmiles();
//returns miles
};
int main()
{
Odometer objODM;
double gal_used(0.0);
char choice;
do
{
objODM.SetMPG(34);
objODM.AddMiles(450);
objODM.AddMiles(95);
objODM.AddMiles(335);
gal_used=objODM.getGallons();
cout << "For " << objODM.getmiles() << " miles ";
cout << gal_used << " gallons were used ";
//asks if user wants to repeat the program
cout << "Do you wish to continue? Y/N ";
cin >> choice;
}
while(choice == 'Y' || choice == 'y');
return 0;
}
void Odometer::SetMPG(double fe)
//sets the mpg based on the fuel effiency
{ if(fe>0 && fe<70)
mpg=fe;
}
void Odometer::AddMiles(double m)
//addes miles to the current miles
{ if(m>0 && m<1000)
miles+=m;
}
void Odometer::reset()
//resets the miles and mpg
{ miles=0.0;
mpg=0;
}
double Odometer::getmiles()
{ return miles;
}
double Odometer::getGallons()
{ return miles/mpg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.