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

Rewrite the program using class. It needs to be able to calculate the quarters f

ID: 3835343 • Letter: R

Question

Rewrite the program using class. It needs to be able to calculate the quarters for each year.

#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 = 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

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

Explanation / Answer

#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