structure problem

Write a program that uses a structure to store the following information on a company division:
Division name (East and West)
Quarter (1, 2, 3, 4)
Quarterly Sales
Total corporate sales for each quarter
Total corporate sales for each division
Total yearly corporate sales

** I do not think that I am defining the variables correctly like (east.firstQtr) in the called functions. How and where should I define those kinds of variables?


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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
 #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;


double setTotSales(double);
double setDivSales(double);
double setAnnualSales(double);
void printStruct(double);

	struct CorpData
	{
    string divisionName;
	    double firstQtr;
	    double secondQtr;
		double thirdQtr;
		double fourthQtr;
		double totalAnnual;
	    double averageQtr;
		
};
 
	int main()
	{
	 
	    CorpData east;
	    CorpData west;
	    
	     
	    fstream corpsales ("corpsales.dat", ios::out);
	    if (!corpsales)
	    {
	        cout << "Error opening file.\n";
	        return 0;
	    }
	    else
	 
	    cout << "Enter the following information about the East Division\n";
	    cout << "Enter the the data for First Quarter :\n";
	    cin >> east.firstQtr;
	  while (east.firstQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.firstQtr;
	  }
	    cout << "Enter the the data for Second Quarter :\n";
	    cin >> east.secondQtr;
	     while (east.secondQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.secondQtr;
	  }
	    cout << "Enter the the data for Third Quarter :\n";
	    cin >> east.thirdQtr;
	    while (east.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.thirdQtr;
	  }
	    cout << "Enter the the data for Fourth Quarter :\n";
	    cin >> east.fourthQtr;
	     while (east.fourthQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> east.fourthQtr;
	  }
	     
	 
	 
	    cout << "Enter the following information about the West Division\n";
	    cout << endl;
	    cout << "Enter the the data for First Quarter :\n";
	    cin >> west.firstQtr;
	    while (west.firstQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.firstQtr;
	  }
	    cout << "Enter the the data for Second Quarter :\n";
	    cin >> west.secondQtr;
	     while (west.secondQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.secondQtr;
	  }
	    cout << "Enter the the data for Third Quarter :\n";
	    cin >> west.thirdQtr;
	     while (west.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.thirdQtr;
	  }
	    cout << "Enter the the data for Fourth Quarter :\n";
	    cin >> west.fourthQtr;
	     while (west.thirdQtr < 0)
	  {
		  cout << "Sales figures cannot be negative. Please re-enter a postive amount\n";
		  cin >> west.fourthQtr;
	  }
	
	     

	    corpsales.write(reinterpret_cast<char *>(&east), sizeof(east));
	    corpsales.write(reinterpret_cast<char *>(&west), sizeof(west));
	    corpsales.close();
	    return 0;
}

// call getDivSales
	double setDivSales()
	{
		double div1, div2, div3, div4;
		div1 = east.firstQtr + west.firstQtr;
		div2 = east.secondQtr + west.secondQtr;
		div3 = east.thirdQtr + west.thirdQtr;
		div4 = east.fourth.Qtr + west.fourthQtr;

	}

// call getAnnualSales
	double setAnnualSales()
	{
		double AnnualSalesEast;
		double AnnualSalesWest;
		AnnualSalesEast = (east.firstQtr + east.secondQtr + east.thirdQtr + east.fourthQtr);
		AnnualSalesWest = (west.firstQtr + west.secondQtr + west.thirdQtr + west.fourth.Qtr);
	}

// call getTotalSales
	double setTotalSales()
	{
		double TotalSales;
			TotalSales = (east.firstQtr = east.secondQtr + east.third.Qtr + east.fourthQtr + west.firstQtr + west.secondQtr + west.thirdQtr + west.fourthQtr);
	}

	


// call printStruct
void printStruct(double)
{
	double AnnualSalesEast, AnnualSalesWest, div1, div2, div3, div4, TotalSales;
	cout << setw(10) << "Div."
			 << setw(10) << "Qtr1"
			 << setw(10) << "Qtr2"
			 << setw(10) << "Qtr3"
			 << setw(10) << "Qtr4"
			 << setw(10) << "TOTAL";
		 cout << setw(10) << "East"
			 << setw(10) << east.firstQtr
			 << setw(10) << east.secondQtr
			 << setw(10) << east.thirdQtr
			 << setw(10) << east.fourthQtr
			 << setw(10) << AnnualSalesEast;
	     cout << setw(10) << "West"
				<< setw(10) << west.firstQtr
				<< setw(10) << west.secondQtr
				<< setw(10) << west.thirdQtr
				<< setw(10) << west.fourthQtr
				<< setw(10) << AnnualSalesWest;
		 cout << setw(10) << "TOTAL"
			 << setw(10) << div1
			 << setw(10) << div2
			 << setw(10) << div3
			 << setw(10) << div4
			 << setw(10) << TotalSales;
}
first thing i don't understand is, where do u call the all these functions in main, that you have defined.

and second its btr to just keep these functions as member functions of the structure and then work on it, or btr yet make a class.

I did the same program, but using classes. Not a great code,but still can be used for reference, i guess!!!



#include<iostream>
#include<string>
#include"wopen.h" //header for keep_window_open()

using namespace std;

class compdiv{

public:

compdiv(){}; //default constructor

void set_qtr_sales();
double first_qtr_sales()const{return first_qtr;}
double second_qtr_sales()const{return second_qtr;}
double third_qtr_sales()const{return third_qtr;}
double fourth_qtr_sales()const{return fourth_qtr;}
double get_total_div_sales();


private:

double first_qtr,second_qtr,third_qtr,fourth_qtr;
};


void compdiv::set_qtr_sales()
{
cout<<"Please enter the sales for first quarter: ";
cin>>first_qtr;
cout<<"Please enter sales for second qtr: ";
cin>>second_qtr;
cout<<"Please enter sales for third qtr: ";
cin>>third_qtr;
cout<<"Please enter sales for fourth qtr: ";
cin>>fourth_qtr;
cout<<endl;
}

double compdiv::get_total_div_sales()
{
return first_qtr+second_qtr+third_qtr+fourth_qtr;
}



int main()
{
compdiv east,west;

cout<<"Enter details for division east:\n";
east.set_qtr_sales();
cout<<"Enter details for division west:\n";
west.set_qtr_sales();

cout<<"Total sales for the first quarter: "<<east.first_qtr_sales()+west.first_qtr_sales()<<endl;
cout<<"Total sales for the second quarter: "<<east.second_qtr_sales()+west.second_qtr_sales()<<endl;
cout<<"Total sales for the third quarter: "<<east.third_qtr_sales()+west.third_qtr_sales()<<endl;
cout<<"Total sales for the fourth quarter: "<<east.fourth_qtr_sales()+west.fourth_qtr_sales()<<endl;

cout<<"Total sale for the east divison: "<<east.get_total_div_sales()<<endl;
cout<<"Total sales for the west division: "<<west.get_total_div_sales()<<endl;

cout<<"Total yearly sales: "<<east.get_total_div_sales()+west.get_total_div_sales()<<endl;

keep_window_open();

return 0;

}


I tried calling them the very first thing before i defined the structure. We're not supposed to use a class so I am trying to do it by calling the functions.
no, where do you call the functions in main?
Topic archived. No new replies allowed.