Structure troubles

Problem; Total Sales and Average Sales displayed by program are coming out as negative values.

Prompt; 10. Corporate Sales Data
Write a program that uses a structure named CorpData to store the following information
on a company division:
Division name (such as East, West, North, or South)
First quarter sales
Second quarter sales
Third quarter sales
Fourth quarter sales

Include a constructor that allows the division name and four quarterly sales amounts to be specified at the time a CorpData variable is created.
The program should create four CorpData variables, each representing one of the
following corporate divisions: East, West, North, and South. These variables should be
passed one at a time, as constant references, to a function that computes the division’s
annual sales total and quarterly average, and displays these along with the division name.

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
#include <iostream>
#include <string>
using namespace std;

struct CorpData
{
	string name;
	double first, second, third, fourth, total, avg;

	CorpData(string n = "", double f = 0, double s = 0, double t = 0, double h = 0)
	{
		name = n;
		first = f;
		second = s;
		third = t;
		fourth = h;
	}

};

void showinfo(CorpData);
void calculate(CorpData);

int main()
{
	CorpData data1("East", 10000, 80000, 20000, 30000);
	CorpData data2("West", 20000, 70000, 10000, 40000);
	CorpData data3("North", 30000, 60000, 20000, 30000);
	CorpData data4("South", 40000, 50000, 10000, 40000);

	CorpData info;

	calculate(data1);
	calculate(data2);
	calculate(data3);
	calculate(data4);

	showinfo(data1);
	showinfo(data2);
	showinfo(data3);
	showinfo(data4);
	system("pause");
	return 0;
}


void showinfo(CorpData part)
{
	cout << "Branch: " << part.name << endl;
	cout << "Total Sales   : " << part.total << endl;
	cout << "Average Sales : " << part.avg << endl;

}
void calculate(CorpData calc)
{
	calc.total = calc.first + calc.second + calc.third + calc.fourth;
	calc.avg = (calc.first + calc.second + calc.third + calc.fourth) / 4;
}
Last edited on
Line 31
Why do you have an object but never use it?

I don't know what the problem is but I fixed it.
You need to pass the structure by reference. This is much more efficient than copying the entire structure.
 
void showinfo(CorpData& part)


Also, you can move the functions inside the structure and call them via an object, just like as you would with a class. A structure is exactly the same as a class, except that by default structure members are public while classes are private.
Like this
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
struct CorpData
{
	string name;
	double first, second, third, fourth, total, avg;

	CorpData(string n = "", double f = 0.0, double s = 0.0, double t = 0.0, double h = 0.0)		//initialise doubles like this
	{
		name = n;
		first = f;
		second = s;
		third = t;
		fourth = h;
	}
	
	void showinfo()
	{
		cout << "Branch: " << name << endl;
		cout << "Total Sales   : " << total << endl;
		cout << "Average Sales : " << avg << endl;
	}
	
	void calculate()
	{
		total = first + second + third + fourth;
		avg = (first + second + third + fourth) / 4;
	}
};


Edit: Commented code right here
https://ideone.com/swKFfV
Last edited on
The problem is that on line 54 you pass calc by value, means that a copy is passed and modified within the function not the original object (like data1, data2....).

You need to pass the data by reference:

void calculate(CorpData &calc) // Note: & -> reference
and the reason that you get negative numbers when you pass the data by value is that you don't initialize the total and avg in the constructor. So they contain garbage and that's what you're getting.
These variables should be passed one at a time, as constant references, to a function that computes the division’s annual sales total and quarterly average, and displays these along with the division name.

They should be passed as constant references:
void calculate(const CorpData &calc)
Also, I interpret this to mean that the one function should calculate and display the total and average. That's actually good because it means you don't have to store them in the class at all, they can be local variables.
Topic archived. No new replies allowed.