parallel arrays

this program is intended to show sales for 4 quarters. am i on the right track?

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
//Include Files
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;

struct comDiv
{
	string divName;
	float sales1;
	float sales2;
	float sales3;
	float sales4;
	float totalDivSales;
};

//Function Prototypes

//Main
int main()
{
	comDiv division[4];

	for (int i=0; i < 4; i++)
	{
		cout << "enter name: ";
		cin >> division[i].divName;

		cout << "enter sales for quarter 1: ";
		cin >> division[i].sales1;

		cout << "enter sales for quarter 2: ";
		cin >> division[i].sales2;

		cout << "enter sales for quarter 3: ";
		cin >> division[i].sales3;

		cout << "enter sales for quarter 4: ";
		cin >> division[i].sales4;
	}	
	
	cout << "------------------------------------------------------" << endl;
	
	for (int i=0; i < 4; i++)
		{
			cout << division[i].divName << endl;
			cout << division[i].sales1 << endl;
			cout << division[i].sales2 << endl;
			cout << division[i].sales3 << endl;
			cout << division[i].sales4 << endl;
		}
		
	int divYear = divYear();
	int totalYear = totalYear();
	int avgQtr = avgQtr();

	getch();
	return 0;
}

//Function Declarations

int divYear();
	cout << division[1].divName
	divYearTotal = division[1].sales1 + division[1].sales2 + division[1].sales3 + division[1].sales4;
	return divYearTotal;
It seems like the way your structure is defined you do not need an array of them.
its supposed to be an array... so how can i fix it to make that happen?
code of totalYear() and avgQtr() is missing if am not mistaken
correct... i need help figuring them out
//Include Files
#include <iostream>
#include <iomanip>
//#include <conio.h>
#include <fstream>

using namespace std;



struct comDiv
{
string divName;
float sales1;
float sales2;
float sales3;
float sales4;
float totalDivSales;
};

float divYear(struct comDiv division)
{
//cout << division[1].divName;
return division.sales1 + division.sales2 + division.sales3 + division.sales4;

// return divYearTotal;
}

//Function Prototypes

//Main
int main()
{
comDiv division[4];

for (int i=0; i < 4; i++)
{
cout << "enter name: ";
cin >> division[i].divName;

cout << "enter sales for quarter 1: ";
cin >> division[i].sales1;

cout << "enter sales for quarter 2: ";
cin >> division[i].sales2;

cout << "enter sales for quarter 3: ";
cin >> division[i].sales3;

cout << "enter sales for quarter 4: ";
cin >> division[i].sales4;
}

cout << "------------------------------------------------------" << endl;

for (int i=0; i < 4; i++)
{
cout << division[i].divName << endl;
cout << division[i].sales1 << endl;
cout << division[i].sales2 << endl;
cout << division[i].sales3 << endl;
cout << division[i].sales4 << endl;
}

float div = divYear(division[1]);
cout << div << endl;


/*int totalYear = totalYear();
int avgQtr = avgQtr();*/

return 0;
}

//Function Declarations
hey there, I've made some changes of your initial code, it has some errors such as:

int divYear = divYear();

first divYear as a function doesn't have a parameter, in which it has to take a parameter which is in your case "division" however division is a struct, so you should define it as a structure parameter in the function.
second, you defined divYear as being int where as struct comDiv defines its elements as float, the addition of floats will give floats, so you either have to convert it into int, or you should change int to float.

as for the array, i guess you're on the right track if you need to input for 4 persons. but if you just need to input for one person, you will not need an array
Is your assignment 1 or 4 divisions with 4 sales?
The following is for 1 division.
If your assignment says you need to use an array I would start with the following:


Changed your structure to

1
2
3
4
5
6
struct comDiv
{
  string divName;
  float sales[4];
  float totalDivSales;
};


Change the start of main to

1
2
3
4
5
6
7
8
9
10
  comDiv division;

  cout << "enter name: ";
  cin >> division.divName;

  for (int i=0; i < 4; i++)
  {
    cout << "enter sales for quarter " << i + 1 << " : ";
    cin >> division.sales[i];
  }	


Apply these hits and post updated code if you still have problems

Last edited on
i've got the main stuff figured out. but now there's some file operations i'm trying to figure out. I'm trying to figure out how to read back code from the saved file to figure out the total yearly corporate sales, etc (see near bottom)

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
//Include Files
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;

struct comDiv
{
	string divName;
	float sales1;
	float sales2;
	float sales3;
	float sales4;
	float totalDivSales;
	float average;
};

//Main
int main()
{
	fstream quarterSales;
	quarterSales.open ("quartersales.txt", ios::out);
	
	float max = -99999999999;
	float min = 99999999999;

	comDiv division[4];

	for (int i=0; i < 4; i++)
	{
		cout << "enter name: ";
		cin >> division[i].divName;

		cout << "enter sales for quarter 1: ";
		cin >> division[i].sales1;
		if (division[i].sales1 > max) 
			max = division[i].sales1;
		if (division[i].sales1 < min) 
			min = division[i].sales1;
		cout << "enter sales for quarter 2: ";
		cin >> division[i].sales2;
		if (division[i].sales2 > max) 
			max = division[i].sales1;
		if (division[i].sales2 < min) 
			min = division[i].sales1;
		cout << "enter sales for quarter 3: ";
		cin >> division[i].sales3;
		if (division[i].sales3 > max) 
			max = division[i].sales3;
		if (division[i].sales3 < min) 
			min = division[i].sales3;
		cout << "enter sales for quarter 4: ";
		cin >> division[i].sales4;
		if (division[i].sales4 > max) 
			max = division[i].sales4;
		if (division[i].sales4 < min) 
			min = division[i].sales4;

		division[i].totalDivSales = division[i].sales1 + division[i].sales2 + division[i].sales3 + division[i].sales4;
		division[i].average = division[i].totalDivSales/4;
	}	
	
	cout << "------------------------------------------------------" << endl;

	cout << "Division\t Sales\t Div. Total\t Div Avg\t" << endl;
	cout << "Qtr 1\t Qtr 2\t Qtr3\t Qtr4\t" << endl;
	for (int i=0; i < 4; i++)
		{
			quarterSales << division[i].divName << " " 
			<< division[i].sales1 << " "
			<< division[i].sales2 << " "
			<< division[i].sales3 << " "
			<< division[i].sales4 << " "
			<< division[i].totalDivSales " "
			<< division[i].average << endl;
			//Div Total
			//Div Avg
		}

	quarterSales.close();	
	cout << "------------------------------------------------------" << endl;

	quarterSales.open("quartersales.txt", ios::in);

	quarterSales >> "Total Yearly Corporate Sales: " >> division[4].totalDivSales >> endl;
	quarterSales >> "Highest Quarter: " >> max >> endl;
	quarterSales >> "Lowest Quarter: " >> min >> endl; 
	
	quarterSales.close();
	getch();
	return 0;
}
my teacher said something about using the max and min values for the high and low quarters, but i have to pass not just the numbers but the names of the division, and the number of the quarter
would you post the given so it'd be easier to understand what ur program is supposed to do exactly?
http://www.2shared.com/document/8MUQG9ly/assignment06.html


it was in pdf form on the website for the class so i had to save it and upload it there.
basically i just need help with the end. it has to basically display the struct. for "highest quarter" it should show the name of the highest quarter, the number of the quarter, and the value in the quarter (and the opposite for lowest)
hey again, here is what i could've done so far, but I didn't test enough for all bugs!

int main()
{
//fstream quarterSales;
//quarterSales.open ("quartersales.txt", ios::out);

float max = 0;
float min = 0;

int flag_high = 0, flag_low = 0 ;
int flag_high_2 = 0, flag_low_2 = 0;
int tmp = 0;


comDiv division[4];


for (int i=0; i < 4; i++)
{
cout << "enter name: ";
cin >> division[i].divName;
for ( int j = 0; j < 4; j++)
{
cout << "enter sales for quarter " << j+1 << " : ";
cin >> division[i].sales[j] ;
division[i].totalDivSales += division[i].sales[j];
if ( i == 0 && j == 0 )
{
tmp = division[0].sales[0];
max = tmp;
min = tmp;
}
if ( division[i].sales[j] > max)
{
flag_high = i;
flag_high_2 = j;
max = division[i].sales[j];
}
else if ( division[i].sales[j] < min)
{
flag_low = i;
flag_low_2 = j;
min = division[i].sales[j];
}
}
division[i].average = division[i].totalDivSales/4.0;
}

cout << "------------------------------------------------------" << endl;

cout << "Division\t Sales\t\t\t Div. Total\t Div Avg\t" << endl;
cout << "\t\tQtr 1\t Qtr 2\t Qtr3\t Qtr4\t" << endl;
for (int i=0; i < 4; i++)
{
cout << division[i].divName << "\t\t";
for (int j=0; j < 4; j++)
cout << division[i].sales[j] << "\t";

cout << division[i].totalDivSales << "\t";
cout << division[i].average << endl;
}

//quarterSales.close();
cout << "------------------------------------------------------" << endl;

//quarterSales.open("quartersales.txt", ios::in);

cout << "Total Yearly Corporate Sales: " << division[4].totalDivSales << endl;

cout << "Highest Quarter: " << endl;
cout << "Division: " << division[flag_high].divName << "\tQuarter: " << flag_high_2 << "\t Sales: " << division[flag_high].sales[flag_high_2]<< endl;
cout << "Lowest Quarter: " << endl;
cout << "Division: " << division[flag_low].divName << "\tQuarter: " << flag_low_2 << "\t Sales: " << division[flag_low].sales[flag_low_2];

//quarterSales.close();
//getch();
return 0;
}


get back to me if you have any question.
my teacher is saying there's a compile error sending information to the array at the end? but idk what she's talking about. it looks ok to me.
nevermind, i forgot to redefine the struct. it works now. thank you so much :]
yes you were right, there were 2 little bugs, here is the correction:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct comDiv
{
string divName;
float sales[4];
float totalDivSales;
float average;

};

int main()
{
float max = 0;
float min = 0;
float tmp = 0;
float total = 0.0;

int flag_high = 0, flag_low = 0 ;
int flag_high_2 = 0, flag_low_2 = 0;

comDiv division[4];

for (int i=0; i < 4; i++)
{
division[i].totalDivSales = 0;
cout << "enter name: ";
cin >> division[i].divName;
for ( int j = 0; j < 4; j++)
{
cout << "enter sales for quarter " << j+1 << " : ";
cin >> division[i].sales[j] ;
division[i].totalDivSales += division[i].sales[j];
if ( i == 0 && j == 0 )
{
tmp = division[0].sales[0];
max = tmp;
min = tmp;
}
if ( division[i].sales[j] > max)
{
flag_high = i;
flag_high_2 = j;
max = division[i].sales[j];
}
else if ( division[i].sales[j] < min)
{
flag_low = i;
flag_low_2 = j;
min = division[i].sales[j];
}
}
division[i].average = division[i].totalDivSales/4.0;
}

cout << "------------------------------------------------------" << endl;
cout << "Division\t Sales\t\t\t Div. Total\t Div Avg\t" << endl;
cout << "\t\tQtr 1\t Qtr 2\t Qtr3\t Qtr4\t" << endl;
for (int i=0; i < 4; i++)
{
cout << division[i].divName << "\t\t";
for (int j=0; j < 4; j++)
cout << division[i].sales[j] << "\t";
cout << division[i].totalDivSales << "\t";
cout << division[i].average << endl;
total += division[i].totalDivSales;
}

cout << "------------------------------------------------------" << endl;
cout << "Total Yearly Corporate Sales: " << total << endl;
cout << "Highest Quarter: " << endl;
cout << "Division: " << division[flag_high].divName << "\tQuarter: " << flag_high_2 << "\t Sales: " << division[flag_high].sales[flag_high_2]<< endl;
cout << "Lowest Quarter: " << endl;
cout << "Division: " << division[flag_low].divName << "\tQuarter: " << flag_low_2 << "\t Sales: " << division[flag_low].sales[flag_low_2];

system("pause");
return 0;
}
the first bug is that we have to initialize totalDivSales to zero, either in the for loop as I did, or in the struct.
division[i].totalDivSales = 0;
the second bug was that you wanted to print out Total Yearly Corporate Sales using division[i].totalDivSales which gives you an error because it doesn't read i, in all cases, even if it reads i, i will be 4, then you will only get the last totalDivSales, whereas you need the sum of 4 of them. so what I did is simply I defined a new element called total, set it to zero, than I added the 4 totalDivSales to it in the last loop.

if you have any question, don't hesitate to get back to me

cheers
Topic archived. No new replies allowed.