简体   繁体   中英

Updating the Total Value with C++ OOP Design

I have a task of calculating helicopter's necessary fuel intake. There are four operations a helicopter can do:

  • Holding
  • Raising
  • Landing
  • Straight

User selects the helicopter's actions by entering the parameters. User can select as many operations as s/he wants. At the end, total fuel is calculated according to the user's parameters and operation selection. I need to create this system with OOP approach.

Here is what I've done so far:

#include <iostream>
#include <stdio.h>
using namespace std;

class FlyingMode {
   protected:
    float time, fuel_rate, start, end, pace, distance;
    float total;
   public:
      FlyingMode(float a=0, float b=0, float c=0,
              float d=0, float e=0, float f=0){
         time = a;
         fuel_rate = b;
         start = c;
         end = d;
         pace = e;
         distance = f;
         total = 0;
      }
      virtual int calcFuel(){
         return 0;
      }
};
class Holding: public FlyingMode{
   public:
      Holding(float a=0, float b=0, float c=0,
              float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }

      int calcFuel(){
         total = (time * fuel_rate * 60);
         return total;
      }
};
class Raising: public FlyingMode{
   public:
      Raising(float a=0, float b=0, float c=0,
                  float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
      int calcFuel (){
          if(start < end && pace != 0 ){
              total = (end - start)/pace * fuel_rate;
              return (end - start)/pace * fuel_rate;
          }else{
              return 0;
          }
      }
};
class Landing: public FlyingMode{
   public:
      Landing(float a=0, float b=0, float c=0,
                  float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
      int calcFuel (){
          if(start > end && pace != 0 ){
              total = (start - end)/pace * fuel_rate;
              return (start - end)/pace * fuel_rate;
          }else{
              return 0;
          }
      }
};
class Straight: public FlyingMode{
   public:
      Straight(float a=0, float b=0, float c=0,
                  float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
      int calcFuel (){
          if(distance != 0 || pace != 0 ){
              total = distance/pace * fuel_rate;
              return distance/pace * fuel_rate;
          }else{
              return 0;
          }
      }
};
// Main function for the program
int main( ){

    float a=0, b=0;
    float c=0, d=0, e=0, f=0;
    float total = 0;
    char op;

    Holding hold;
    Raising raise;
    Landing land;
    Straight straight;

    while(op != 'x') {

        cout << "Enter the move : " << endl;
        cout << "1 ---> Holding Flight" << endl;
        cout << "2 ---> Raising" << endl;
        cout << "3 ---> Landing " << endl;
        cout << "4 ---> Straight Flight " << endl;
        cout << "5 ---> Calculate fuel" << endl;
        cout << "x ---> Exit " << endl;

        op = std::getchar();

        if(op == '1') {
            cout << "Enter time : ";
            cin >> a;
            cout << "Enter fuel rate: ";
            cin >> b;
        }
        if(op == '2') {
            cout << "Enter starting altitude: ";
            cin >> c;
            cout << "Enter ending altitude: ";
            cin >> d;
            cout << "Enter raising pace: ";
            cin >> e;
            cout << "Enter fuel rate: ";
            cin >> f;
        }
        if(op == '3') {
            cout << "Enter starting altitude:  ";
            cin >> c;
            cout << "Enter ending altitude:  ";
            cin >> d;
            cout << "Enter landing pace:  ";
            cin >> e;
            cout << "Enter fuel rate:  ";
            cin >> b;
        }
        if(op == '4') {
            cout << "Enter ending altitude: ";
            cin >> d;
            cout << "Enter starting altitude: ";
            cin >> c;
            cout << "Enter fuel rate: ";
            cin >> b;
        }
        if(op == '5') {
            FlyingMode *mode;

            hold = Holding(a, b, 0, 0, 0, 0);
            raise = Raising(0, f, c, d, e, 0);
            land = Landing(0, b, d, c, e, 0);
            straight = Straight(0, b, c, d, 0, 0);

            //store holding address

            //call holding fuel
            mode = &hold;
            float hold_result = mode -> calcFuel();
            //call raising fuel
            mode = &raise;
            float raise_result = mode -> calcFuel();
            //call landing fuel
            mode = &land;
            float land_result = mode -> calcFuel();
            //call straight fuel
            mode = &straight;
            float str_result = mode -> calcFuel();
            //calculate total

            /*cout << "hold_result" << hold_result;
            cout << "raise_result" << raise_result;
            cout << "land_result" << land_result;
            cout << "str_result" << str_result;*/
            total = hold_result + raise_result + land_result + str_result;
            cout <<"Total required fuel : "<< total << " kg/second "<< endl;

        }
        if(op == 'x') {
            cout << "System will exit..." << endl;
            return 0;
        }
        else {
            //if(op==(1|2|3|4|5)){}
            //else cout << "Wrong selection." << endl;
            }
    }
    return 0;

}

But, it only calculates the total value for the user's first operation selection. Any ideas on how to solve this?

First, use switch instead of if whenever possible that would make your code faster, don;t repeat yourself.I see repetitive instructions...

The quick fix here,I suggest you move all your variables inside the while loop so that they will not be overwritten; or if that is your intention, have some more extra variables so that they will be uniquely assigned to each of your cin statement. But still, you should improve your code design.

You are calculating fuel only if op is 5. So only the last action is being calculated. You have to calculate total each time a user enters a action. I have modified the code. Check if it works and mark it as an answer so that the question can be closed.

#include <iostream>
#include <stdio.h>
using namespace std;

class FlyingMode {
   protected:
    float time, fuel_rate, start, end, pace, distance;
    float total;
   public:
      FlyingMode(float a=0, float b=0, float c=0,
              float d=0, float e=0, float f=0){
         time = a;
         fuel_rate = b;
         start = c;
         end = d;
         pace = e;
         distance = f;
         total = 0;
      }
      virtual int calcFuel(){
         return 0;
      }
};
class Holding: public FlyingMode{
   public:
      Holding(float a=0, float b=0, float c=0,
              float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) {                                  }
int calcFuel(){
         total = (time * fuel_rate * 60);
     return total;
  }
};
class Raising: public FlyingMode{
   public:
      Raising(float a=0, float b=0, float c=0,
                  float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
      int calcFuel (){
          if(start < end && pace != 0 ){
              total = (end - start)/pace * fuel_rate;
          return (end - start)/pace * fuel_rate;
      }else{
          return 0;
      }
  }
};
class Landing: public FlyingMode{
   public:
      Landing(float a=0, float b=0, float c=0,
                  float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
      int calcFuel (){
          if(start > end && pace != 0 ){
              total = (start - end)/pace * fuel_rate;
              return (start - end)/pace * fuel_rate;
          }else{
              return 0;
          }
      }
};
class Straight: public FlyingMode{
   public:
  Straight(float a=0, float b=0, float c=0,
              float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
  int calcFuel (){
      if(distance != 0 || pace != 0 ){
          total = distance/pace * fuel_rate;
          return distance/pace * fuel_rate;
      }else{
          return 0;
      }
  }
};
// Main function for the program
int main( ){

float a=0, b=0;
float c=0, d=0, e=0, f=0;
float total = 0;
char op;

Holding hold;
Raising raise;
Landing land;
Straight straight;

while(op != 'x') {

    cout << "Enter the move : " << endl;
    cout << "1 ---> Holding Flight" << endl;
    cout << "2 ---> Raising" << endl;
    cout << "3 ---> Landing " << endl;
    cout << "4 ---> Straight Flight " << endl;
    cout << "5 ---> Calculate fuel" << endl;
    cout << "x ---> Exit " << endl;

    cin>>op;
    cout<<"op="<<op<<endl;
    if(op == '1') {
        cout << "Enter time : ";
        cin >> a;
        cout << "Enter fuel rate: ";
        cin >> b;
    }
    else if(op == '2') {
        cout << "Enter starting altitude: ";
        cin >> c;
        cout << "Enter ending altitude: ";
        cin >> d;
        cout << "Enter raising pace: ";
        cin >> e;
        cout << "Enter fuel rate: ";
        cin >> f;
    }
    else if(op == '3') {
        cout << "Enter starting altitude:  ";
        cin >> c;
        cout << "Enter ending altitude:  ";
        cin >> d;
        cout << "Enter landing pace:  ";
        cin >> e;
        cout << "Enter fuel rate:  ";
        cin >> b;
    }
    else if(op == '4') {
        cout << "Enter ending altitude: ";
        cin >> d;
        cout << "Enter starting altitude: ";
        cin >> c;
        cout << "Enter fuel rate: ";
        cin >> b;
    }
    else if(op == '5') {

        cout <<"Total required fuel : "<< total << " kg/second "<< endl;

    }
    else if(op == 'x') {
        cout << "System will exit..." << endl;
        return 0;
    }
    else {
        //if(op==(1|2|3|4|5)){}
        //else cout << "Wrong selection." << endl;
      continue;
        }
        FlyingMode *mode;

        hold = Holding(a, b, 0, 0, 0, 0);
        raise = Raising(0, f, c, d, e, 0);
        land = Landing(0, b, d, c, e, 0);
        straight = Straight(0, b, c, d, 0, 0);

        //store holding address

        //call holding fuel
        mode = &hold;
        float hold_result = mode -> calcFuel();
        //call raising fuel
        mode = &raise;
        float raise_result = mode -> calcFuel();
        //call landing fuel
        mode = &land;
        float land_result = mode -> calcFuel();
        //call straight fuel
        mode = &straight;
        float str_result = mode -> calcFuel();
        //calculate total

        /*cout << "hold_result" << hold_result;
        cout << "raise_result" << raise_result;
        cout << "land_result" << land_result;
        cout << "str_result" << str_result;*/
        total = total+hold_result + raise_result + land_result + str_result;
        cout <<"Total required fuel : "<< total << " kg/second "<< endl;

}
return 0;

}

Did you check whether you are entering in to the function - calcFuel(). Because you have put the conditions. I tried with following values and got the different answers: Landing - a=0, b=24, c=76, d=46, e=43, f=0 flying - a=0, b=24, c=76, d=46, e=43, f=0 raising - a=0, b=21, c=46, d=76, e=43, f=0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM