Home > database >  I tried doing a do while loop in this code but every time it loops back the rate increases is there
I tried doing a do while loop in this code but every time it loops back the rate increases is there

Time:04-29

The problem with this code is that every time I loop, it back to start the rate increases and don't stay at a fix value of 0.10. like I finish my first run, the second time I repeat the code, the rate went from 25% as the last commission rate to 30%, the starting commission rate, to the new loop even.

My desire output only is:

Enter the sales: 2500

10% commission : Php 2500.00

15% commission : Php 3750.00

20% commission : Php 5000.00

25% commission : Php 6250.00

any fix for this?

#include<iostream>
using namespace std;

class Gas_commission{
    private:
        float res_coms = 0, val_sales = 0, val_rate = 0.10;
    public:
        void get_info(){
            //input
            cout << "-------------------------------------" << endl;
            cout << "Enter the following to find the commission: " << endl;
            cout << "-------------------------------------" << endl;
            cout << "Enter Sales: ";
                cin >> val_sales;
            cout << "\n";
        }
        void get_res(){
            //output
            cout << "-------------------------------------" << endl;
            for (int i = 1; i <= 4; i  ){
            res_coms = val_sales * val_rate;
            cout << val_rate * 100 << "% " << "commission: Php " << res_coms << endl;
            val_rate  = 0.05;  //the rate here keep increasing every time I loop the code
            }
        }
};
int main()
{
  Gas_commission SALE;
  char input;
  do{
    SALE.get_info();
    SALE.get_res();
    cout << "-------------------------------------" << endl;
    cout << "\nDo you want to retry again (Y/N)? ";
        cin >> input;
  } while (input == 'Y' || input == 'y');
}

CodePudding user response:

One of the simplest solutions is to "reset" the variable val_rate to its starting default value in each call to get_res():

void get_res(){
    val_rate = 0.10;

    // The rest of the code...
}

By doing this, the variable no longer needs to be a member variable, it can be a local variable inside the get_res function instead, which makes more sense. The result variable res_coms should be local as well.


Another option is to define the SALE variable inside your loop. Then it will be constructed and all the variables be reset to their default values each iteration of the loop.

Actually, I think a combination would probably be best: Define the res_coms and val_rate variables locally inside the get_res function, and define SALE inside the loop.

CodePudding user response:

I think Gas_commission SALE; should put in the do while loop to reinitialize your private attribute.

CodePudding user response:

You have two options:

  • In your current Gas_commission class, the val_rate variable is only initialized to 0.1 once in Gas_commission's constructor. To reset it each time get_res is called, you need to reset it at the start of that function:

    void get_res()
    {
         val_rate = 0.1;
         // ...
    }
    

    And while you're at that, note how val_rate is actually only used inside get_res! so you can move it from the class to the function; i.e.:

    class Gas_commission{
      private:
        float res_coms = 0, val_sales = 0;
          // ...
          void get_res()
          {
               float val_rate = 0.1;
               // ...
          }
    };
    
  • An alternative to all of the above is to put the SALE object into your loop, then also the constructor (and therefore val_rate's initialization) is done in each loop iteration:

    int main()
    {
      char input;
      do{
        Gas_commission SALE;
        SALE.get_info();
        SALE.get_res();
        cout << "-------------------------------------" << endl;
        cout << "\nDo you want to retry again (Y/N)? ";
          cin >> input;
      } while (input == 'Y' || input == 'y');
    }
    
  •  Tags:  
  • c
  • Related