Help with a class

Hey everyone! I'm having trouble with my program. It runs... but I can't get it to give me the total when I call it. As far as I can tell, it's only totaling one divisions quarterly sales, not all the divisions. I'm at a bit of a loss, so any help is welcome. It's probably something simple and I'm just stuck in the fish bowl and not able to see the issue...

Here's the header code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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:

    //Gets and stores the quarterly sales, then totals it 
    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 getTCorpSales() 
    { return totalSales; } 
}; 
  
#endif 


Source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

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


void Q(double, double, double, double); 
double numQuarters(int);            
double getTCorpSales();             

double DivSales::totalSales; 


int main () { 
    
  
	//V&C
    DivSales div[6];            
    double quarter[4];          

    
	cout << "	------------------------------------------------------"<<endl;
    cout << "	| Welcome to the Corporation Quarterly Sales program |" << endl;
	cout << "	------------------------------------------------------"<<endl;

	//Input
    for(int count1=0; count1 < 6; count1++)
    { 

		cout << endl;
        cout << "Please enter the sales for Division " << (count1 + 1) << ": " << endl;
        cout << endl;

		for (int i=0; i < 4; i++)
        {
            cout << "Quarter " << (i +1) << ": ";
            cin >> quarter[i];

			//Validation
			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]); 

			

        } 
    } 
	
	cout << fixed << showpoint << setprecision(2)<<endl;
	
	//Output
	cout <<endl<< "		Company Sales"<<endl;
	cout << endl;
	cout << "----------------------------------------------------------------------"<<endl;
	
	//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 << "	Quarter " << (test + 1) << ": $" << div[count].numQuarters(test) << endl;
		}
	}
	cout << endl;
    
	//list total sales from DivSales class function
    cout << "		Total Sales for Company:	$ " << div[0].getTCorpSales() << endl;
    cout << "----------------------------------------------------------------------"<<endl;
 
    return 0; 
}
Last edited on
I think you need to return total sales from

void Q();
Can you explain? I thought since totalSales was a static variable, it was accessible to all the other functions. More specifically, to:
double getTCorpSales()
{ return totalSales; }

div[count1].Q(quarter[0], quarter[1], quarter[2], quarter[3]);

This does nothing.

You could do

div[count1] = Q();

Or something like that. When you do, return the total sales in the function.
Topic archived. No new replies allowed.