I have started working with structures so here's a side project from my text book. It's purpose is fairly simple; it asks for the sales of each quarter of the year from 4 different divisions and then calculates the average quarterly sales and total annual sales and finally displays all the data. My problem is that in the function "displayCompanyInfo" the statement
Sales figures for division: North
Enter 1st quarter sales: 3
Enter 2nd quarter sales: 3
Enter 3rd quarter sales: 3
Enter 4th quarter sales: 3
Sales figures for division: South
Enter 1st quarter sales: 4
Enter 2nd quarter sales: 4
Enter 3rd quarter sales: 4
Enter 4th quarter sales: 4
Sales figures for division: East
Enter 1st quarter sales: 5
Enter 2nd quarter sales: 5
Enter 3rd quarter sales: 5
Enter 4th quarter sales: 5
Sales figures for division: West
Enter 1st quarter sales: 6
Enter 2nd quarter sales: 6
Enter 3rd quarter sales: 6
Enter 4th quarter sales: 6
Sales Figures by Division
Division
First quarter sales: 3
Second quarter sales: 3
Third quarter sales: 3
Fourth quarter sales: 3
Total annual sales: 12
Average quarterly sales: 3
Division
First quarter sales: 4
Second quarter sales: 4
Third quarter sales: 4
Fourth quarter sales: 4
Total annual sales: 16
Average quarterly sales: 4
Division
First quarter sales: 5
Second quarter sales: 5
Third quarter sales: 5
Fourth quarter sales: 5
Total annual sales: 20
Average quarterly sales: 5
Division
First quarter sales: 6
Second quarter sales: 6
Third quarter sales: 6
Fourth quarter sales: 6
Total annual sales: 24
Average quarterly sales: 6
Press any key to continue . . .
As you can see the last part of the output has statements that say "Division" however they do not say the name of the division afterwards. I don't understand why that is?
getCompanyInfo is quite wasteful:
- Since the parameter R is passed by value, it makes a copy of the parameter.
- Then you assign quarterly values to temp
- (it looks like temp gets copied to the reutrn value, but the compiler probably optimizes
this away, allocating temp in the location of the return value).
- then you copy the returned CompanyInfo object into the original.
Just pass the object to getCompanyInfo by reference: void getCompanyInfo(CompanyInfo &dest);