structures functions

Corporate Data Sales

Write a program that uses a structure named CorpData to store the following information on a company division.

Division Name(North, South, East, Or West)
First quarter sales
Second quarter sales
Third quarter sales
Fourth quarter sales
Total annual sales
Average 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 variables of this structure, each representing one of the following corporate divisions: North, South, East and West. Each variable should be passed in turn to a function that calculates and stores the total annual sales and average sales for that division.

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

Hers my 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
100
101
102
103
104
105
106
107
// Reaper

#include <iostream>
using namespace std;

struct CorpData
{
    string north,
           south,
           east,
           west;

    float FirstQuartSales,
          SecondQuartSales,
          ThirdQuartSales,
          FourthQuartSales,
          TotalAnnualSales,
          AvgQuartSales;

    CorpData (string n, string s, string e, string w, float a, float b, float c, float d)
    {
        north = n;
        south = s;
        east = e;
        west = w;

        FirstQuartSales = a;
        SecondQuartSales = b;
        ThirdQuartSales = c;
        FourthQuartSales = d;
    }
};

void FindTotal(CorpData x)
{
    float total;

    total = x.FirstQuartSales + x.SecondQuartSales + x.ThirdQuartSales + x.FourthQuartSales;
    x.TotalAnnualSales = total;

    return;
}

void FindAverage(CorpData y)
{
    float average;

    average = (y.FirstQuartSales + y.SecondQuartSales + y.ThirdQuartSales + y.FourthQuartSales) / 4;
    y.AvgQuartSales = average;

    return;
}

int main()
{
    CorpData north{"North", north.FirstQuartSales, north.SecondQuartSales, north.ThirdQuartSales, north.FourthQuartSales};
    CorpData south{"South", south.FirstQuartSales, south.SecondQuartSales, south.ThirdQuartSales, south.FourthQuartSales};
    CorpData east{"East", east.FirstQuartSales, east.SecondQuartSales, east.ThirdQuartSales, east.FourthQuartSales};
    CorpData west{"West", west.FirstQuartSales, west.SecondQuartSales, west.ThirdQuartSales, west.FourthQuartSales};

    cout << "The will calculate and display quarterlys.\n\n";

    cout.setf(ios_base::fixed , ios_base::floatfield);
    cout.precision(2);

    cout << "North Division" << endl;
    cout << "Enter first quarter sales: $";
    cin >> north.FirstQuartSales;
    cout << "Enter second quarter sales: $";
    cin >> north.SecondQuartSales;
    cout << "Enter third quarter sales: $";
    cin >> north.ThirdQuartSales;
    cout << "Enter fourth quarter sales: $";
    cin >> north.FourthQuartSales;

    cout << "\nSouth Division" << endl;
    cout << "Enter first quarter sales: $";
    cin >> south.FirstQuartSales;
    cout << "Enter second quarter sales: $";
    cin >> south.SecondQuartSales;
    cout << "Enter third quarter sales: $";
    cin >> south.ThirdQuartSales;
    cout << "Enter fourth quarter sales: $";
    cin >> south.FourthQuartSales;

    cout << "\nEast Division" << endl;
    cout << "Enter first quarter sales: $";
    cin >> east.FirstQuartSales;
    cout << "Enter second quarter sales: $";
    cin >> east.SecondQuartSales;
    cout << "Enter third quarter sales: $";
    cin >> east.ThirdQuartSales;
    cout << "Enter fourth quarter sales: $";
    cin >> east.FourthQuartSales;

    cout << "\nWest Division" << endl;
    cout << "Enter first quarter sales: $";
    cin >> west.FirstQuartSales;
    cout << "Enter second quarter sales: $";
    cin >> west.SecondQuartSales;
    cout << "Enter third quarter sales: $";
    cin >> west.ThirdQuartSales;
    cout << "Enter fourth quarter sales: $";
    cin >> west.FourthQuartSales;

    return 0;
}


Im pretty sure this is all wrong, im not going to lie im confused right now. Can someone help point me in the right direction on starting this program, im more confused with passing structure data to functions.

So i would like to know if someone can help me start this program, atleast get me through the structure data to functions part of the coding.
firstly you should understand what do you like to do;
Seems that you fully not understand what struct/class is.
Please read chapter about it again.
The program should create four variables of this structure, each representing one of the following corporate divisions: North, South, East and West.
Fine, you've created north, south, east and west. But the CorpData struct/class should just have one name, not four as in:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct CorpData
{
    string north,  // These are wrong, there should be just one name.
           south,
           east,
           west;

    float FirstQuartSales,
          SecondQuartSales,
          ThirdQuartSales,
          FourthQuartSales,
          TotalAnnualSales,
          AvgQuartSales;

Each variable should be passed in turn to a function that calculates and stores the total annual sales and average sales for that division.
You've done these, but you need to pass the CorpData by reference. For the functions should start as:
1
2
void FindTotal(CorpData &x)
void FindAverage(CorpData &y)

each variable should be passed in turn to a function that displays the division name, total sales, and average sales.You need to create yet another function to do the display.
Ok, from what im readin this is what it looks like to me, heres my new code so far.

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
// Reaper

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct CorpData
{
	private:
		string name;

		float FirstQuartSales,
			  SecondQuartSales,
			  ThirdQuartSales,
			  FourthQuartSales,
			  TotalAnnualSales,
			  AvgQuartSales;
	public:

		void storeData(string n, float a, float b, float c, float d);
		void getData();
		void findTotal();
		void findAverage();
		void displayData();
};

void CorpData::storeData(string n, float a, float b, float c, float d)
{
	name = n;
	FirstQuartSales = a;
	SecondQuartSales = b;
	ThirdQuartSales = c;
	FourthQuartSales = d;
}

void CorpData::getData()
{
	cout << "North Division" << endl;
	cout << "Enter first quarterly amount: $";
	cin >> north.FirstQuartSales;
	cout << "Enter second quarterly amount: $";
	cin >> north.SecondQuartSales;
	cout << "Enter third quarterly amount: ";
	cin >> north.ThirdQuartSales;
	cout << "Enter fourth quarterly amount: ";
	cin >> north.FourthQuartSales;

	cout << "\nSouth Division" << endl;
	cout << "Enter first quarterly amount: $";
	cin >> south.FirstQuartSales;
	cout << "Enter second quarterly amount: $";
	cin >> south.SecondQuartSales;
	cout << "Enter third quarterly amount: ";
	cin >> south.ThirdQuartSales;
	cout << "Enter fourth quarterly amount: ";
	cin >> south.FourthQuartSales;

	cout << "\nEast Division" << endl;
	cout << "Enter first quarterly amount: $";
	cin >> east.FirstQuartSales;
	cout << "Enter second quarterly amount: $";
	cin >> east.SecondQuartSales;
	cout << "Enter third quarterly amount: ";
	cin >> east.ThirdQuartSales;
	cout << "Enter fourth quarterly amount: ";
	cin >> east.FourthQuartSales;

	cout << "\nWest Division" << endl;
	cout << "Enter first quarterly amount: $";
	cin >> west.FirstQuartSales;
	cout << "Enter second quarterly amount: $";
	cin >> west.SecondQuartSales;
	cout << "Enter third quarterly amount: ";
	cin >> west.ThirdQuartSales;
	cout << "Enter fourth quarterly amount: ";
	cin >> west.FourthQuartSales;

	findTotal();
	findAverage();
	displayData();
}

int main()
{
	cout << "This calculates and displays quarterlys.\n\n";



	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

    return 0;
}


Am i heading in the right direction? If not can someone give me a really good reference for this type of problem.
Does anyone know of a reference where i can read more about this type of program and examples.
Have you thought about creating a Sales class? Something like this, where you assign the class it's division upon creation. This way, you can easily add new divisions by adding code to only 2 places and you can cut down a lot of redundant 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
class Sales
{
public:
	enum Division { NORTH, SOUTH, EAST, WEST };
	
	// you must assign a division on creation	
	Sales(Division d) { _division = d; }

	
	void GetData(istream& in)
	{
		cout << DivToString() << " Division" << endl;
		cout << "Enter first quarterly amount: $";
		cin >> _FirstQuart;
		cout << "Enter second quarterly amount: $";
		cin >> _SecondQuart;
		cout << "Enter third quarterly amount: ";
		cin >> _ThirdQuart;
		cout << "Enter fourth quarterly amount: ";
		cin >> _FourthQuart;
	}	
	
private:
	string DivToString()
	{
		switch(_division)
		{
			case NORTH : return "North";
				break;
			case SOUTH : return "South";
				break;
			case EAST : return "East";
				break;
			case WEST : return "West";
				break;
                        default: return "No Division";
		}
	}

	float _FirstQuart;
	float _SecondQuart;
	float _ThirdQuart;
	float _FourthQuart;
	float _AvgQuart;
	float _TotalAnnual;
	Division _division;
};


You would declare an individual sale like

Sales sale1(Sales::NORTH);


If you want to declare a container full of Sales then you will have to add a default constructor.
Last edited on
You seem to have changed so much between the first and second versions of the program.

All that i/o stuff doesn't belong in the class. The class just has the data, methods to get/set that data and optionally perform calculations on that data.
Topic archived. No new replies allowed.