### 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107`` ``````// Reaper #include 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.
 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:
 ``12345678910111213`` ``````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:
 ``12`` ``````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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````// Reaper #include "stdafx.h" #include #include 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.
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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````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.