Struct of array

Need to output the winning division sales of a company's region using struct and an array. I'm having trouble with compiling it. I think i'm close just can't seem to get over this "hump". Any comments/criticism welcome! Thank you!

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


void populate_div_sales(DIV[], int);
int findHighest (DIV[], int);
void print_result(DIV[], int);

struct DIV 
{
	float sale;
	string name;
} div_info[4];

int main ()
{
	
	
	DIV div_info.name = {"Northeast", "Northwest", "Southeast", "Southwest"};
	
	populate_div_sales(div_info, 4);
	
	winner = int findHighest(div_info, 4);
	
	print_result(div_info, 4);
	
	return 0;
}


	void print_result(DIV div_info, int 4)
	{
		for(int i = 0; i < 4; i++)
		{
			cout << array[i] << endl;
		}
	}

	void populate_div_sales(DIV div_info, int 4)
	{
 		for(int i =0; i < 4; i++) {
 			cout << "Enter region sales for " << div_info.name[i] << ": $ ";
			cin >> div_info.sale[i];
			while (div_info.sale[i]<=0){
		}
	}

	Int findHighest(DIV div_info.sale, int 4) 
	{ 
		float greatestSalesAmount = 0; 
		int save_index; 
		
		for(int i=0; i < 4; i++) 
		{
			if(div_info.sale[i] > greatestSalesAmount) 
			greatestSalesAmount = div_info.sale[i];
			save_index = div_info.sale[i]; 
		} 
return save_index; 
} 		


I can't really figure out how to recognize my mistakes in order to correct them.

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
prog.cpp:7:25: error: variable or field 'populate_div_sales' declared void
 void populate_div_sales(DIV[], int);
                         ^
prog.cpp:7:25: error: 'DIV' was not declared in this scope
prog.cpp:7:29: error: expected primary-expression before ']' token
 void populate_div_sales(DIV[], int);
                             ^
prog.cpp:7:32: error: expected primary-expression before 'int'
 void populate_div_sales(DIV[], int);
                                ^
prog.cpp:8:18: error: 'DIV' was not declared in this scope
 int findHighest (DIV[], int);
                  ^
prog.cpp:8:22: error: expected primary-expression before ']' token
 int findHighest (DIV[], int);
                      ^
prog.cpp:8:25: error: expected primary-expression before 'int'
 int findHighest (DIV[], int);
                         ^
prog.cpp:8:28: error: expression list treated as compound expression in initializer [-fpermissive]
 int findHighest (DIV[], int);
                            ^
prog.cpp:9:19: error: variable or field 'print_result' declared void
 void print_result(DIV[], int);
                   ^
prog.cpp:9:19: error: 'DIV' was not declared in this scope
prog.cpp:9:23: error: expected primary-expression before ']' token
 void print_result(DIV[], int);
                       ^
prog.cpp:9:26: error: expected primary-expression before 'int'
 void print_result(DIV[], int);
                          ^
prog.cpp: In function 'int main()':
prog.cpp:21:14: error: expected initializer before '.' token
  DIV div_info.name = {"Northeast", "Northwest", "Southeast", "Southwest"};
              ^
prog.cpp:23:32: error: 'populate_div_sales' was not declared in this scope
  populate_div_sales(div_info, 4);
                                ^
prog.cpp:25:2: error: 'winner' was not declared in this scope
  winner = int findHighest(div_info, 4);
  ^
prog.cpp:25:11: error: expected primary-expression before 'int'
  winner = int findHighest(div_info, 4);
           ^
prog.cpp:27:26: error: 'print_result' was not declared in this scope
  print_result(div_info, 4);
                          ^
prog.cpp: At global scope:
prog.cpp:33:38: error: expected ',' or '...' before numeric constant
  void print_result(DIV div_info, int 4)
                                      ^
prog.cpp: In function 'void print_result(DIV, int)':
prog.cpp:37:12: error: 'array' was not declared in this scope
    cout << array[i] << endl;
            ^
prog.cpp: At global scope:
prog.cpp:41:44: error: expected ',' or '...' before numeric constant
  void populate_div_sales(DIV div_info, int 4)
                                            ^
prog.cpp: In function 'void populate_div_sales(DIV, int)':
prog.cpp:45:26: error: invalid types 'float[int]' for array subscript
    cin >> div_info.sale[i];
                          ^
prog.cpp:46:26: error: invalid types 'float[int]' for array subscript
    while (div_info.sale[i]<=0){
                          ^
prog.cpp:50:2: error: 'Int' was not declared in this scope
  Int findHighest(DIV div_info.sale, int 4) 
  ^
prog.cpp:62:1: error: expected '}' at end of input
 }  
 ^

Last edited on
For the first problem look at this snippet:
1
2
3
4
5
6
7
8
9
void populate_div_sales(DIV[], int);
int findHighest (DIV[], int);
void print_result(DIV[], int);

struct DIV 
{
	float sale;
	string name;
} div_in


The function prototypes don't know about your structure since the structure definition follows the prototypes. Move the structure declaration to above the prototypes.

Also I suggest you stop using that global variable (div_in) and make it local to a function and pass it to and from your functions as necessary.



Wow, moving struct before the prototypes cleared up half my errors. I will begin to change div_info to work locally, thank you for your help!
I think the concept of structures and arrays is confusing to me. I set up an array and when I fill the array with say one part of the struct and then go to fill it with the other part, i feel this won't work out. I mean I feel that I still need two arrays, but my instructions were to do it with one, "DIV []". But how do I fill it with information for the sale and then the name? 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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct DIV {
	float sale;
	string name;
} div_info [3];

void populate_div_sales(DIV[], int);
int findHighest (DIV[], int);
void print_result(DIV[], int);

int main ()
{
	
	div_info.name = {"Northeast", "Northwest", "Southeast", "Southwest"};
	
	populate_div_sales(div_info, 4);
	
	winner = int findHighest(div_info, 4);
	
	print_result(div_info, 4);
	
	return 0;
}


	void print_result(DIV div_info, int 4)
	{
		for(int i = 0; i < 4; i++)
		{
			cout << array[i] << endl;
		}
	}

	void populate_div_sales(DIV div_info, int 4)
	{
 		for(int i =0; i < 4; i++) {
 			cout << "Enter region sales for " << div_info.name[i] << ": $ ";
			cin >> div_info.sale[i];
			while (div_info.sale[i]<=0){
		}
	}

	Int findHighest(DIV div_info.sale, int 4) 
	{ 
		float greatestSalesAmount = 0; 
		int save_index; 
		
		for(int i=0; i < 4; i++) 
		{
			if(div_info.sale[i] > greatestSalesAmount) 
			greatestSalesAmount = div_info.sale[i];
			save_index = div_info.sale[i]; 
		} 
return save_index; 
}
Any help please
Remove div_info [3] from line 9.

On line 18:
1
2
3
4
5
DIV div_info [] = { { 0, "Northeast" }, { 0, "Northwest" }, etc. };
// Note: The compiler calculates the size:
const int div_size = (sizeof(div_info) / sizeof(*div_info));	

	populate_div_sales(div_info, div_size);


The second parameter makes no sense:
1
2
3
4
5
6
7
	void print_result(DIV div_info, int size) // Note: size instead of 4
	{
		for(int i = 0; i < size; i++) // Note: size instead of 4
		{
			cout << array[i] << endl;
		}
	}
The same applies to the other functions.
There's so much temptation to just erase and start over, but I feel like I'm almost there!
Here's my update. Are the headaches a part of this coding process. jk :P
Any help much appreciated and criticism is welcomed!

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

struct DIV {
	float sale;
	string name;
} ;

void populate_div_sales(DIV[], int);
int findHighest (DIV[], int);
void print_result(DIV[], int);

int main ()
{
	int winner;
	
	DIV div_info [] = { { 0, "Northeast" }, { 0, "Northwest" }, { 0, "Southwest" }, { 0, "Southeast" } };
	
	const int div_size = (sizeof(div_info) / sizeof(*div_info));	

	populate_div_sales(div_info, div_size);
	
	winner = findHighest(div_info, div_size);
	
	print_result(div_info, div_size);
	
	return 0;
}


	void print_result(DIV div_info, int size)
	{
		for(int i = 0; i < size; i++)
		{
			cout << div_info [i] << endl;
		}
	}

	void populate_div_sales(DIV div_info, int size)
	{
 		for(int i =0; i < size; i++) {
 			cout << "Enter region sales for " << div_info.name[i] << ": $ ";
			cin >> div_info.sale[i];
			while (div_info.sale[i]<=0){
		}
	}

    findHighest(DIV div_info.sale, int size) 
	{ 
		float greatestSalesAmount = 0; 
		int save_index; 
		
		for(int i=0; i < size; i++) 
		{
			if(div_info.sale[i] > greatestSalesAmount) 
			greatestSalesAmount = div_info.sale[i];
			save_index = div_info.sale[i]; 
		} 
return save_index; 
}




Errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
prog.cpp: In function 'void print_result(DIV, int)':
prog.cpp:37:21: error: no match for 'operator[]' (operand types are 'DIV' and 'int')
    cout << div_info [i] << endl;
                     ^
prog.cpp: In function 'void populate_div_sales(DIV, int)':
prog.cpp:45:26: error: invalid types 'float[int]' for array subscript
    cin >> div_info.sale[i];
                          ^
prog.cpp:46:26: error: invalid types 'float[int]' for array subscript
    while (div_info.sale[i]<=0){
                          ^
prog.cpp:50:21: error: expected primary-expression before 'div_info'
     findHighest(DIV div_info.sale, int size) 
                     ^
prog.cpp:50:36: error: expected primary-expression before 'int'
     findHighest(DIV div_info.sale, int size) 
                                    ^
prog.cpp:62:1: error: expected '}' at end of input
 }
 ^
Last edited on
The signature of print_result(...) and the other functions are wrong:

void print_result(DIV div_info[], int size) // Note: add []


Line 37: you cannot cout an arbitrary struct (unless you implemented an operator<<(...) for this purpose).

So something like:
1
2
cout << "name: " << div_info[i].name << endl;
cout << "sale: " << div_info[i].sale << endl;


Note the position of [i]. You need to change that in all other cases where you use div_info as well.

AND: The signature of findHighest(...) is the same as the other functions.
Last edited on
I see why i needed to do DIV div_info [] = { { 0, "Northeast"}...}
But here are the changes. Thank you for your guidance, it is making it clear for what I was doing wrong so far in my labels and calls.

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

struct DIV {
	float sale;
	string name;
} ;

void populate_div_sales(DIV[], int);
int findHighest (DIV[], int);
void print_result(DIV[], int);

int main ()
{
	int winner;
	
	DIV div_info[] = { { 0, "Northeast" }, { 0, "Northwest" }, { 0, "Southwest" }, { 0, "Southeast" } };
	
	const int div_size = (sizeof(div_info) / sizeof(*div_info));	

	populate_div_sales(div_info[], div_size);
	
	winner = findHighest(div_info[], div_size);
	
	print_result(div_info[], div_size);
	
	return 0;
}


	void print_result(DIV div_info[], int size)
	{
		for(int i = 0; i < size; i++)
		{
			cout << "name: " << div_info[i].name << endl;
                        cout << "sale: " << div_info[i].sale << endl;
		}
	}

	void populate_div_sales(DIV div_info[], int size)
	{
 		for(int i =0; i < size; i++) {
 			cout << "Enter region sales for " << div_info[i].name << ": $ ";
			cin >> div_info[i].sale;
			while (div_info[i].sale <=0){
		}
	}

    findHighest(DIV div_info[], int size) 
	{ 
		float greatestSalesAmount = 0; 
		int save_index; 
		
		    for(int i=0; i < size; i++) 
		   {
		        if(div_info[i].sale > greatestSalesAmount) 
		        greatestSalesAmount = div_info[i].sale;
			    save_index = div_info[i].sale; 
	       } 
        return save_index; 
}}


New errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
prog.cpp: In function 'int main()':
prog.cpp:23:30: error: expected primary-expression before ']' token
  populate_div_sales(div_info[], div_size);
                              ^
prog.cpp:25:32: error: expected primary-expression before ']' token
  winner = findHighest(div_info[], div_size);
                                ^
prog.cpp:27:24: error: expected primary-expression before ']' token
  print_result(div_info[], div_size);
                        ^
prog.cpp: In function 'void populate_div_sales(DIV*, int)':
prog.cpp:51:21: error: expected primary-expression before 'div_info'
     findHighest(DIV div_info[], int size) 
                     ^
prog.cpp:51:33: error: expected primary-expression before 'int'
     findHighest(DIV div_info[], int size) 
                                 ^
Topic archived. No new replies allowed.