/* Filename: odometer.cpp
 * Author: Tom Pizzo
 * Email address: pizzotm@clarkson.edu
 * Description: Program to track fuel and mileage for an automotive vehicle.
 * Last changed: Dec 1, 2008
 */

#include <iostream>

using namespace std;

const char YES = 'y';

class Odometer {

public:
  Odometer ();
  //Constructor function to initialize values to zero.

  void  input();
  //Recieves input from user.
  void set_mile (double new_miles, double new_fuel);
  //resets Fuel and miles according to arguments.
  void set_odo(double temp_odometer);

  void output(double gas_burned);
  //Displays how much gas is burned from the input of the user.

  void get_fuel_efficiency(double temp_odometer,double new_miles, double& gas_burned);

  double add_to_odometer(double temp_odometer, double new_miles );
  //Takes miles driven on the trip and adds them to the odometer.
  char play_again(char y_or_n);
  //Prompts user to enter another trip.
  double reset_odo(double temp_odometer, char reset_variable);
  //Prompts user to reset odometer or not.


private:

  double miles_driven;
  double fuel_efficiency;

};

int main (){

  Odometer trip;
  char y_or_n;
  char variable,reset_variable;
  double gas_burned;
  double new_miles,new_fuel,temp_odometer,new_odo;

  do{

    trip.input();
    trip.set_mile(new_miles,new_fuel);
    //trip.set_odo(temp_odometer);
    //trip.add_to_odometer(new_miles,temp_odometer);
    trip.get_fuel_efficiency(temp_odometer,new_fuel,gas_burned);
    trip.output(gas_burned);
    variable = trip.play_again(y_or_n);

    if (variable == YES){
      trip.reset_odo(temp_odometer,reset_variable);

    }


  }while(variable == YES);

  return 0;

}

//Function definitions.

Odometer::Odometer() {

  miles_driven = 0.0;
  fuel_efficiency = 0.0;
  odometer = 0.0;
return;

}

void Odometer::input(){
  using namespace std;

  cout << "Tell me about your trip!";
  cout << "\nFuel efficiency(MPG)?";

  cin >> fuel_efficiency;

  cout << "\nMiles driven?";

  cin >> miles_driven;
  return;

}

void Odometer::set_mile(double new_miles, double new_fuel){

  miles_driven = new_miles;
  fuel_efficiency = new_fuel;
  return;
}

void Odometer::set_odo(double temp_odometer){

  temp_odometer = odometer;

 return;
}
double Odometer::add_to_odometer(double temp_odometer,double new_miles){

  temp_odometer = odometer + new_miles;


  return te

}

void Odometer::get_fuel_efficiency(double temp_odometer, double new_fuel,double& gas_burned){

  gas_burned = milestemp_odometer / new_fuel;

  return;
}
void Odometer::output(double gas_burned){

  using namespace std;

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);

  cout << "You've burned "<<gas_burned<<" of gas so far.";

  return;

}

char Odometer::play_again(char y_or_n){

  cout <<"\nAdd another trip?";

  cin >> y_or_n;

  return y_or_n;

  }

 double Odometer::reset_odo(double temp_odometer,char reset_variable){

   cout<<"Reset odometer(y,n)?";

   cin>> reset_variable 
;
   if (reset_variable == YES){

     temp_odometer = 0;
   }

   else

     temp_odometer = temp_odometer;

   return temp_odometer;

 }

It should take in miles driven and the fuel efficiency of the trip and return how many gallons of gas has been used from the trip. It will then prompt the user if they want to add another trip and reset the odometer. Please help, I'm having a hard time bugging this. Just need another set of eyes to look at it.

When?...

trip.input();
trip.set_mile(new_miles,new_fuel);

The first one reads input and assigns the class member vars.

The second just trashes the data with the uninitialised values in main. Just delete this line.

/* Filename: odometer.cpp
 * Author: Tom Pizzo
 * Email address: pizzotm@clarkson.edu
 * Description: Program to track fuel and mileage for an automotive vehicle.
 * Last changed: Dec 1, 2008
 */

#include <iostream>

using namespace std;

const char YES = 'y';

class Odometer {

public:
  Odometer ();
  //Constructor function to initialize values to zero.

  void  input();
  //Recieves input from user.
  void set_mile (double &new_miles, double &new_fuel);
  //resets Fuel and miles according to arguments.
  void set_odo(double &temp_odometer);

  void output(double gas_burned);
  //Displays how much gas is burned from the input of the user.

  void get_fuel_efficiency(double temp_odometer,double new_miles, double& gas_burned);

  double add_to_odometer(double &temp_odometer, double new_miles );
  //Takes miles driven on the trip and adds them to the odometer.
  char play_again(char y_or_n);
  //Prompts user to enter another trip.
  double reset_odo(double &temp_odometer, char reset_variable);
  //Prompts user to reset odometer or not.


private:

  double miles_driven;
  double fuel_efficiency;
  double odometer;

};

int main (){

  Odometer trip;
  char y_or_n;
  char variable,reset_variable;
  double gas_burned;
  double new_miles,new_fuel,temp_odometer,new_odo;

  do{

    trip.input();
    trip.set_mile(new_miles,new_fuel);
    trip.set_odo(temp_odometer);
    //trip.add_to_odometer(new_miles,temp_odometer);
    trip.get_fuel_efficiency(temp_odometer,new_fuel,gas_burned);
    trip.output(gas_burned);
    variable = trip.play_again(y_or_n);

    if (variable == YES){
      trip.reset_odo(temp_odometer,reset_variable);

    }


  }while(variable == YES);

  return 0;

}

//Function definitions.

Odometer::Odometer() {

  miles_driven = 0.0;
  fuel_efficiency = 0.0;
  odometer = 0.0;
return;

}

void Odometer::input(){
  using namespace std;

  cout << "Tell me about your trip!";
  cout << "\nFuel efficiency(MPG)?";

  cin >> fuel_efficiency;

  cout << "\nMiles driven?";

  cin >> miles_driven;
  return;

}

void Odometer::set_mile(double &new_miles, double &new_fuel){

  new_miles=miles_driven;
  new_fuel=fuel_efficiency;
}

void Odometer::set_odo(double &temp_odometer){

  std::cout<<"Set new odo, please! : ";
  cin>>odometer;
  temp_odometer = odometer;


}
double Odometer::add_to_odometer(double &temp_odometer,double new_miles){

  temp_odometer = odometer + new_miles;


  return 0.0;

}

void Odometer::get_fuel_efficiency(double temp_odometer, double new_fuel,double& gas_burned){

  gas_burned = temp_odometer / new_fuel;


}
void Odometer::output(double gas_burned){

  using namespace std;

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);

  cout << "You've burned "<<gas_burned<<" of gas so far.";

  return;

}

char Odometer::play_again(char y_or_n){

  cout <<"\nAdd another trip?";

  cin >> y_or_n;

  return y_or_n;

  }

 double Odometer::reset_odo(double &temp_odometer,char reset_variable){

   cout<<"Reset odometer(y,n)?";

   cin>> reset_variable 
;
   if (reset_variable == YES){

     temp_odometer = 0;
   }

   

      

   return 0.0;

 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.