I have a code and its almost done. Its for a school project and I am stuck. I have DivSales and a CorporateSales.cpp. The programs should ask the user for 6 divisions quarterly sales. Display the results into a table and give a total of sales. My total feature is not working through my class. Hre is the code:

DivSales.h:

#ifndef DIVSALES_H
#define DIVSALES_H 
#include <iostream> 

//DivSales class delcaration 
class DivSales 
{ 
    private: 
    static double totalSales;      //static member variable for total sales 
    double quarters[4];             //variable to hold quarters  
  
    public:

    //Mutator function to hold quarter sales and get totalSales for each quarter
    void Q(double q0, double q1, double q2, double q3)  
    { 
        quarters[0]=q0; 
        quarters[1]=q1; 
        quarters[2]=q2; 
        quarters[3]=q3; 
        totalSales =+ q0+q1+q2+q3; 
    } 

    //accessor function to get quarters
    double numQuarters(int quart) 
    { return quarters[quart]; } 

    //accessor function to get total sales for all quarters
    double getTotalSales() 
    { return totalSales; } 
}; 
  
#endif

CorporateSales.cpp:

#include <iostream> 
#include <iomanip> 
#include "DivSales.h" 
using namespace std; 

//get function from DivSales class
void Q(double, double, double, double); 
double numQuarters(int);            //call from DivSale 
double getTotalSales();             //call from DivSale


//link to total sales in DivSales Class Header file
double DivSales::totalSales; 

int main () { 
    
  
    DivSales div[6];            //variable to hold 6 Divisions 
    double quarter[4];          //varialbe to hold 4 quarters


    //for loop to prompt for 6 divisions 4 quarters sales
    cout << "PLEASE ENTER SALES FOR EACH DIVISION'S 4 QUARTERS" << endl;
    for(int count1=0; count1 < 6; count1++)
    { 
        //request for Division office sales
        cout << " " << endl;
        cout << "Please enter the sales for Division " << (count1 + 1) << ": " << endl;
        cout << " " << endl;

        //loop for sales input per quarter
        for (int i=0; i < 4; i++)
        {
            cout << "Quarter " << (i +1) << " Sales: ";
            cin >> quarter[i];

            //validate input for sales is greater than 0
            while (quarter[i] < 0)
            {
                cout << "Please enter a number greater than or equal to 0: ";
                cin >> quarter[i];
            }
        }

  
        //storing numbers into div array for future calling
        div[count1].Q(quarter[0], quarter[1], quarter[2], quarter[3]); 
    } 

    //display results with formatting
    cout << fixed << showpoint << setprecision(2);

    //giving results
   cout << "\nHere are the total sales for the company\n";
   cout << " " << endl;
   cout << "----------------------------------------------------------------------\n";

   //for loop to give division and quarter sales table
   for (int count = 0; count < 6; count++)
   {
       //list division
       cout << "Division " << (count + 1) << ": " << endl; 

       //list 4 quarters of sales
       for (int test= 0; test < 4; test++)
       {
           cout << "\tQuarter " << (test + 1) << ": $" << div[count].numQuarters(test) << endl;
       }
   }
    cout << "" << endl;         //spacer

    //list total sales from DivSales class function
    cout << "\t\tTotal Sales for Company: \t$ " << div[0].getTotalSales() << endl;
    cout << "----------------------------------------------------------------------\n";
    cin.ignore(); 
    cin.get(); 
    return 0; 
}

any help would be GREAT. Thak you in advance as always.

I wouldn't have written it in the same way, but I think your problem is two-fold:

First, you never set an initial value for DivSales::totalSales so it's initial value is undefined.

Second, line 21 of DivSales.h where you think you are adding to the total,

totalSales =+ q0+q1+q2+q3;
// This is the same as
totalSales =      (+q0) + q1 + q2 + q3;
// I think you wanted
totalSales += q0 + q1 + q2 + q3;
// That is the same as
totalSales = totalSales + q0 + q1 + q2 + q3;

I wouldn't have the class 'own' a total for all the instances. I would have each instance of the DivSales class total its own sales (so it would know all 4 quarters and its own total). I would then add functionality to be able to accumulate DivSales within another DivSales. You would initialize the 'company' DivSales to zeros and then add each of the Divisions DivSales as it is filled in. You could at that point then display the company totals for each quarter and an overall value.

For tabular output I would expect to output something like the following:

Division        Q1      Q2      Q3      Q4      Total
    1          225     242     334     326       1127
    2          100     100     100     100        400
--------     -----   -----   -----   -----    -------
Total          325     342     434     426       1527
This article has been dead for over six months. Start a new discussion instead.