struct and constructors and all that niffty stuff. halp?

closed account (2EURX9L8)
Hi friends ! I has a question. I am not sure I should be doing all this at the first but I am not understanding the assignment

the assignment (incoming wall of txt):

Write a program that uses a structure named CorporateData to store the following information on a company division:

Division name (such as East, West, etc.)
First quarter sales
Second quarter sales
Third quarter sales
Fourth quarter sales
Total annual sales
Average quarterly sales

Include a constructor that allows the division name and four quarterly sales amounts to be specified at the time a CorporateData variable is created.

The program should create four variables of this structure, each representing one of the following corporate divisions: East, Central, Mountain, and West. Use the following sales data:

East
4876957.32
6349845.61
3456978.97
5348976.44

Central
2465948.58
3459123.25
4012648.38
3945372.76

Mountain
7843123.33
8364891.97
6159487.54
8974561.27

West
12345941.14
9874561.87
10315648.25
11594843.81

Each variable should be passed in turn to a function that calculates and stores the total annual sales and average quarterly sales for that division. Once this has been done for each division, each variable should be passed in turn to a function that displays to the screen the division name, total annual sales, and quarterly average.


This is the code I have started with but am stuck on if I should be doing it this way or not. What do you guys think"? I haven't started passing them to a function as I want to get this first part right. any way the 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
#include<iostream>
#include<string>

using namespace std;
double computeAnnsales(int, double, double, double, double);
double computeAvrage(int, double, double, double, double);
struct CorporateData{
	string division1,division2, division3, division4,east, west, central, mountain;
		double totalAnnsales, averageQsales;
		double firstQsalesEast,secondQsalesEast,thirdQsalesEast,fourthQsalesEast;
		double firstQsaleswest,secondQsaleswest,thirdQsaleswest,fourthQsaleswest;
		double firstQsalescentral,secondQsalescentral,thirdQsalescentral,fourthQsalescentral;
		double firstQsalesmountain,secondQsalesmountain,thirdQsalesmountain,fourthQsalesmountain;
	CorporateData (){
			division1=east;
			firstQsalesEast=4876957.32;
			secondQsalesEast=6349845.61;
			thirdQsalesEast=3456978.97;
			fourthQsalesEast=5348976.44;
			
			
			division2=west;
			firstQsaleswest=12345941.14;
			secondQsaleswest=9874561.87;
			thirdQsaleswest=10315648.25;
			fourthQsaleswest=11594843.81;
			
				
			division3=central;
			firstQsalescentral=2465948.58;
			secondQsalescentral=3459123.25;
			thirdQsalescentral=4012648.38;
			fourthQsalescentral=3945372.76;
			

			division4=mountain;
			firstQsalesmountain=7843123.33;
			secondQsalesmountain=8364891.97;
			thirdQsalesmountain=6159487.54;
			fourthQsalesmountain=8974561.27;
	};


	double computeAnnsales(int, double, double, double, double)
	{	



Last edited on
closed account (2EURX9L8)
Am I going overboard on the variables?
Your structure contains variables for all 4 regions. You want a structure that contains variables for 1 region and then have 4 instances of that structure. Here is a start.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct CorporateData
{
  string DivisionName;
  double FirstQuarterSales;
  double SecondQuarterSales;
  double ThirdQuarterSales;
  double FourthQuarterSales;
  double TotalAnnualSales;
  double AverageQuarterlySales;
};

// You will have 4 of the following statement (one for each region)
struct CorporateData westRegion(parameters to your constructor go here);
Last edited on
closed account (2EURX9L8)
I had that at first but the instructions are confusing me with the "Include a constructor that allows the division name and four quarterly sales amounts to be specified at the time a CorporateData variable is created."

do you think I need to do a struct and construct for every region like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct CorporateData west(double firstQsales, double secondQsales, double thirdQsales, double fourthQsales, double &totalANNsales, double &averageQsales)
	{double firstQsaleswest=12345941.14,
				secondQsaleswest=9874561.87,
				thirdQsaleswest=10315648.25,
				fourthQsaleswest=11594843.81;
   
double computeAnnsales( double firstQsales, double secondQsales, double thirdQsales, double fourthQsales, double &totalANNsales, double &averageQsales);
			   totalANNsales=firstQsales+secondQsales+thirdQsales+fourthQsales;
			   return totalANNsales;

			  double main();
			  {cout<<totalANNsales;
			  
			  return;}

Last edited on
closed account (2EURX9L8)
By the way thank you guys a bunch, my class is so early in the morning I never know whats going on, and the text is not very helpful but you guys sure are.
"Include a constructor that allows the division name and four quarterly sales amounts to be specified at the time a CorporateData variable is created."

It means this;

CorporateData variable_name1(division_name1, 1.2, 2.3, 1.3, 2.5, 2.6, 2.5);
CorporateData variable_name2(division_name2, 1.2, 2.3, 1.3, 2.5, 2.6, 2.5);
CorporateData variable_name3(division_name3, 1.2, 2.3, 1.3, 2.5, 2.6, 2.5);

this is "specified at the time a CorporateData variable is created."
I think 'Include a constructor that allows the division name and four quarterly sales amounts to be specified at the time a CorporateData variable is created' means you need to create a default constructor.
closed account (2EURX9L8)
Ya I am still stuck, I thought reading up and notes would help but I am as lost as I was at the start.

How do I go about the constructor to use different division names?

1
2
3
4
5
CorporateData()
{	DivName="";
}

CorporateData variable_name1("west", 1.2, 2.3, 1.3, 2.5, 2.6, 2.5);


??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct CorporateData {
  string division;
  double FirstQuarterSales;
  double SecondQuarterSales;
  double ThirdQuarterSales;
  double FourthQuarterSales;
  double TotalAnnualSales;
  double AverageQuarterlySales;
  CorporateData(string, double, double, double, double, double, double);
};

CorporateData::CorporateData(string a, double b, double c, double d, double e, double f, double g){
  division = a;
  FirstQuarterSales = b;
  SecondQuarterSale = c;
  ThirdQuarterSales = d;
  FourthQuarterSales = e;
  TotalAnnualSales = f;
  AverageQuarterlySales = g;
}
Last edited on
I would be more tempted to go with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct CorporateData
{
    std::string DivisionName ;
    double QSales[4] ;
    double AnnualSales ;
    double AvgQSales ;

    CorporateData(std::string name, double q1, double q2, double q3, double q4 ) ;  
};

CorporateData(std::string name, double q1, double q2, double q3, double q4)
    : DivisionName(name), AnnualSales(q1+q2+q3+q4), AvgQSales(AnnualSales/4)
{
    QSales[0] = q1 ;
    QSales[1] = q2 ;
    QSales[2] = q3 ;
    QSales[3] = q4 ;
}


Note that Annual Sales and Average Quarterly Sales can be calculated from the information given to the constructor, which is why the assignments specifies passing only the name and quartlerly sales to the constructor.

Edit: Actually, looking more closely at the assignment, AnnualSales and AvgQSales shouldn't be calculated here. You should initialize them to 0, and then pass the structs to a function to calculate those values (which is very silly from a design perspective, but it's what is asked for.)

Last edited on
closed account (2EURX9L8)
Am I understanding that I need to make a variable for each division ie... CorporateData westDivi("west",12345941.14,9874561.87,10315648.25,11594843.81); , where do I put them?? in main? in the constructor?

I need to do a proto type like, void annualsales(string, double, double, double, double);

then pass the variables to the function like, void annualsales(westAnn,eastAnn,centralAnn,mountainAnn)??

closed account (2EURX9L8)
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
struct CorporateData {
  string division;
  double FirstQuarterSales;
  double SecondQuarterSales;
  double ThirdQuarterSales;
  double FourthQuarterSales;
  double computeAnnsales;
  double computeAvgsales;
  //CorporateData(string, double, double, double, double, double, double);


CorporateData(string division, double b, double c, double d, double e,double f, double g){
  
 //CorporateData westDivi("west",12345941.14,9874561.87,10315648.25,11594843.81);
	division="";
	FirstQuarterSales = b;
	SecondQuarterSales = c;
	ThirdQuarterSales = d;
	FourthQuarterSales =e ;
	computeAnnsales=0;
	computeAvgsales=0;
}
}



double computeAvrage(string, double, double, double, double, double, double);	
void computeAnnsales(CorporateData)	

int main()
{
	CorporateData westDivi("west",12345941.14,9874561.87,10315648.25,11594843.81,0,0);
	computeAvrage(westDivi);
	cout<<"The avrage sales for the"<<CorporateData.division<<"was "<<

return 0;
}



double computeAvrage(double)
{
	double westAvg;
	westAvg=(b+c+d+e)/4;
	return westAvg;
}


Last edited on
Am I understanding that I need to make a variable for each division ie... CorporateData westDivi("west",12345941.14,9874561.87,10315648.25,11594843.81); , where do I put them?? in main? in the constructor?

I need to do a proto type like, void annualsales(string, double, double, double, double);


Yes, and no.

You'll be wanting to declare those variables in main.

1
2
// your assignment requires the constructor take 5 values, not 7.
CorporateData West( "West",  #,  #,  #,  # ) ; 


The program should create four variables of this structure,
...
Each variable should be passed in turn to a function that calculates and stores the total annual sales and average quarterly sales for that division


So you'll need a function with a signature similar to:

void DoCalcs( CorporateData & cd ) ;


Topic archived. No new replies allowed.