class problems
Dec 9, 2012 at 3:39pm UTC
I am getting multiple errors,the first is in int main under switch case 4, it says meter must have a point to function and i have no idea what that means or what i have to do for that. second issue is void tracker::input ( ), it is saying tracker::input can't be re declared outside of its class, but when i ran this program without the switch statements this error wasn't occurring.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <iostream>
using namespace std;
class tracker
{
public :
void input ( );
void efficiency ( );
void set (int newtrips, double newmiles, double newgas);
void set (double miles, double gas);
void set (int changemiles);
double getgas( );
double getmiles( );
double efficiency;
private :
int trips;
double miles, gas;
};
int main ()
{
int x;
tracker meter;
meter.input ( );
cout << "What would you like to do?" << endl;
cout << "1. Enter a new set of trips, mileage and gas usage" << endl;
cout << "2. Output fuel used" << endl;
cout << "3. Output mileage" << endl;
cout << "4. Output fuel efficiency" << endl;
cout << "5. Exit" << endl;
cout << "Your choice: " ;
cin >> x;
switch (x)
{
case 1:
meter.input ( );
case 2:
meter.getgas( );
case 3:
meter.getmiles( );
case 4:
meter.efficiency ( );
}
void tracker::input ( )
{
cout << "How many trips do you wish to make? " ;
cin >> trips;
cout << "How many miles in total ae you driving? " ;
cin >> miles;
cout << "How many gallons of gas are used? " ;
cin >> gas;
}
void tracker::efficiency ( )
{
efficiency = miles/gas;
return efficiency;
}
void tracker::set(int newtrips, double newmiles, double newgas)
{
cout << "How many trips do you wish to make? " ;
cin >> trips;
cout << "How many miles in total ae you driving? " ;
cin >> miles;
cout << "How many gallons of gas are used? " ;
cin >> gas;
}
double tracker::getgas( )
{
return gas;
}
double tracker::getmiles( )
{
return miles;
}
Dec 9, 2012 at 4:27pm UTC
Several issues.
One, there is no closing brace for
switch (x) {
in function
main()
.
In class tracker, the name "efficiency" is declared twice, first as the name of a member function, then as the name of a variable.
In the definition of that function, there are related problems.
1 2 3 4 5
void tracker::efficiency ( )
{
efficiency = miles/gas;
return efficiency;
}
First, that same doubly-defined name is referenced again. Second, a void function does not return a value.
You could try something like this:
1 2 3 4
void tracker::efficiency ( )
{
eff = miles/gas;
}
Here,
eff
is simply a renamed version of the original variable. The member variable of the class will need to be renamed to match.
Dec 9, 2012 at 5:21pm UTC
thank you for the help, silly me forgot to put the close bracket but now another issue. I have to use an accessor function to print out total miles and total gas, here is what i have under a switch case
1 2 3
meter.getgas( );
system ("pause" );
break ;
and the accessor to go with it
1 2 3 4
double tracker::getgas( )
{
return gas;
}
my problem is i can't get it to print out without moving gas to public instead of remaining private where the prof wants it
Dec 9, 2012 at 5:28pm UTC
Do you mean you tried this but it didn't work:
1 2 3
cout << meter.getgas( );
system ("pause" );
break ;
(there was no
cout in the code you posted).
Topic archived. No new replies allowed.