Why is this structure member not displaying?

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
1
2
 
	std::cout << "Division " << R.division_name << std::endl;
does not display the name of the division. With that in mind here is 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>

struct CompanyInfo
{
	std::string division_name;
	double first_qtr_sales;
	double second_qtr_sales;
	double third_qtr_sales;
	double fourth_qtr_sales;
	double total_annual_sales;
	double avg_qtrly_sales;
};

CompanyInfo getCompanyInfo(CompanyInfo);
void displayCompanyInfo(CompanyInfo);

const int QTRS = 4;
const int DIVISIONS = 4;

int main()
{
	CompanyInfo companies[DIVISIONS];


	std::string division_names[DIVISIONS] = { "North", "South", "East", "West" };


	for (int count = 0; count < DIVISIONS; count++)
	{
		companies[count].division_name = division_names[count];
		companies[count] = getCompanyInfo(companies[count]);
	}


	std::cout << "\t\t\tSales Figures by Division\n";
	std::cout << "\t\t\t-------------------------\n";

	for (int count = 0; count < DIVISIONS; count++)
		displayCompanyInfo(companies[count]);
	
}

bool noNegatives(double x)
{
	bool status = false;
	if (x < 0.0)
		std::cout << "Negative numbers are not accepted.\n";
	else
		status = true;
	return status;
}

CompanyInfo getCompanyInfo(CompanyInfo R)
{
	CompanyInfo temp;
	
	std::cout << "Sales figures for division: " << R.division_name << std::endl;

	do{
		std::cout << "Enter 1st quarter sales: ";
		std::cin >> temp.first_qtr_sales;
	} while ( ! noNegatives(temp.first_qtr_sales));

	do{
		std::cout << "Enter 2nd quarter sales: ";
		std::cin >> temp.second_qtr_sales;
	} while ( ! noNegatives(temp.second_qtr_sales));

	do{
		std::cout << "Enter 3rd quarter sales: ";
		std::cin >> temp.third_qtr_sales;
	} while ( ! noNegatives(temp.third_qtr_sales));

	do{
		std::cout << "Enter 4th quarter sales: ";
		std::cin >> temp.fourth_qtr_sales;
	} while ( ! noNegatives(temp.fourth_qtr_sales));

	std::cout << "\n";

	temp.total_annual_sales = temp.first_qtr_sales + temp.second_qtr_sales
							+ temp.third_qtr_sales + temp.fourth_qtr_sales;
	temp.avg_qtrly_sales = temp.total_annual_sales / QTRS;

	return temp;
}

void displayCompanyInfo(CompanyInfo R)
{
	std::cout << "Division " << R.division_name << std::endl;
	std::cout << "First quarter sales: " << R.first_qtr_sales << std::endl;
	std::cout << "Second quarter sales: " << R.second_qtr_sales << std::endl;
	std::cout << "Third quarter sales: " << R.third_qtr_sales << std::endl;
	std::cout << "Fourth quarter sales: " << R.fourth_qtr_sales << std::endl;
	std::cout << "Total annual sales: " << R.total_annual_sales << std::endl;
	std::cout << "Average quarterly sales: " << R.avg_qtrly_sales << std::endl;
	std::cout << "\n";
}

And here is the output:
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
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?
You never set your division name in the structure at all. so it will be blank.

You are returning 'temp' in your getCompanyInfo method. temp gets assigned all of the numbers, but you never set temp's division name.

adding this on line 59 'fixes' it:
temp.division_name = R.division_name;

but this isn't great. at the moment you only pass in R to use it's name which is a bit weird. if you see what I mean.

you could just pass in the division name string, use this to set temp's name, then return temp.
Last edited on
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);
Topic archived. No new replies allowed.