Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rewrite the program using class . int years;//amount of years and quarters doubl

ID: 3835302 • Letter: R

Question

Rewrite the program using class.

   int years;//amount of years and quarters

   double sales = 0;

   double nsales = 0;

   cout << "This program will calculate avergae sales over a period of years. How many years do you wish to average? ";

while((cin >> years) && (years == 0)){//invalid

   cin.clear();//clear and reinput

   cout << "Years must be at least one. Please re-enter: ";//> 1

   }//quarters

       for(years; years >= y; y++){

           cout << "Year " << y << endl;//start quarters with years

           cout << "Sales amount for quarter 1? ";//quarter 1

           while((cin >> sales) && (sales < 0)){//invalid input of negatives

               cout << "Sales amount must be zero or greater" << endl;//nuuuu

           cin.clear();//clear and reinput

           cout << "Sales amount for quarter 1? ";//input amount for 1

           }

           nsales += sales;

           cout << "Sales amount for quarter 2? ";// quarter 2

           while((cin >> sales) && (sales < 0)){//invalid input of negatives

               cout << "Sales amount must be zero or greater" << endl;//nuuuu

           cin.clear();//clear and reinput

           cout << "Sales amount for quarter 2? ";// input amount for 2

           }

           nsales += sales;

           cout << "Sales amount for quarter 3? ";//quarter 3

           while((cin >> sales) && (sales < 0)){//invalid input of negatives

               cout << "Sales amount must be zero or greater" << endl;//nuuu

           cin.clear();//clear and reinput

           cout << "Sales amount for quarter 3? ";// input amount for 3

           }

           nsales += sales;

           cout << "Sales amount for quarter 4? ";//quarter 4

           while((cin >> sales) && (sales < 0)){//invalid input of negatives

               cout << "Sales amount must be zero or greater" << endl;//nuuuu

           cin.clear();//clear and reinput

           cout << "Sales amount for quarter 4? ";// input amount for 4

           }

           nsales += sales;

       }//formula

       double avg = nsales/(years*4);

       cout << "Over a period of " << years*4 << " quarters: " << fixed << setprecision(2) << nsales << endl;//setprecision fixed

       cout << "Average quartely sales for the period is: " << fixed << setprecision(2) << avg << endl;//setprecision fixed

}//end

Explanation / Answer

So as you have requested, to encapsulate the method within a class, I did it for you. Plese let me know if you have any issue with the same

#include<iostream>
#include<iomanip>
using namespace std;

class Test {
public:
void doCalculation(int years) //amount of years and quarters)
{

    double sales = 0;

    double nsales = 0;

    cout << "This program will calculate avergae sales over a period of years. How many years do you wish to average? ";

    while((years < 0)){//invalid

        cin.clear();//clear and reinput

        cout << "Years must be at least one. Please re-enter: ";//> 1
      
       cin >> years;

    }//quarters

        for(int y = 1; years >= y; y++){

            cout << "Year " << y << endl;//start quarters with years

            cout << "Sales amount for quarter 1? ";//quarter 1

            while((cin >> sales) && (sales < 0)){//invalid input of negatives

                cout << "Sales amount must be zero or greater" << endl;//nuuuu

                cin.clear();//clear and reinput

                cout << "Sales amount for quarter 1? ";//input amount for 1

            }

            nsales += sales;

            cout << "Sales amount for quarter 2? ";// quarter 2

            while((cin >> sales) && (sales < 0)){//invalid input of negatives

                cout << "Sales amount must be zero or greater" << endl;//nuuuu

                cin.clear();//clear and reinput

                cout << "Sales amount for quarter 2? ";// input amount for 2

            }

            nsales += sales;

            cout << "Sales amount for quarter 3? ";//quarter 3

            while((cin >> sales) && (sales < 0)){//invalid input of negatives

                cout << "Sales amount must be zero or greater" << endl;//nuuu

                cin.clear();//clear and reinput

                cout << "Sales amount for quarter 3? ";// input amount for 3

            }

            nsales += sales;

            cout << "Sales amount for quarter 4? ";//quarter 4

            while((cin >> sales) && (sales < 0)){//invalid input of negatives

                cout << "Sales amount must be zero or greater" << endl;//nuuuu

                cin.clear();//clear and reinput

                cout << "Sales amount for quarter 4? ";// input amount for 4

            }

            nsales += sales;

        }//formula

    

double avg = nsales/(years*4);

        cout << "Over a period of " << years*4 << " quarters: " << fixed << setprecision(2) << nsales << endl;//setprecision fixed

        cout << "Average quartely sales for the period is: " << fixed << setprecision(2) << avg << endl;//setprecision fixed


}
};//end

int main() {
   int years;
   cout<<"Enter years: ";
   cin >> years;
   Test t;
   t.doCalculation(years);  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote