structure array error

The code contain error, i wanted to declare my array with value just with ={}
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
#include <iostream>
using namespace std;


struct Proverty_Guidelines
	{
		int family_size;
		int income;
		
	}Survey[8];

void main ()
{
	

	{
		Survey[8].family_size={1,2,3,4,5,6,7,8};
		Survey[8].income={10210,13690,17170,20650,24130,27610,31090,34570};
	}
	{
		cout<<"Family Size"<<Survey[8].family_size<<endl;
		cout<<"Income"<<Survey[8].income;
	
	}
	
}
I suppose you want to fill all the elements in the array. That's not possible the way you do it. You need a for loop:
1
2
3
4
for ( int i = 0; i < 8; ++i )
{
    Survey[ i ].family_size = i;
}


And with this: Survey[ 8 ] you try to access all elements of the array at the same time, but that's not possible. What you actually do is accessing the ninth element of the array (remember the first element is on position 0), while the array is only 8 elements long -> out of bounds crash.
Last edited on
thx for the reply, that mean i need to declare 1 by 1 ? or there is another way because i need to declare and for loop is not suitable. Can the code for declare can be shorten ??
1
2
family_size={1,2,3,4,5,6,7,8};
income={10210,13690,17170,20650,24130,27610,31090,34570};
You mean like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Proverty_Guidelines
	{
		int family_size;
		int income;
		
	}Survey[8] = {
                                         { 1, 10210 },
                                         { 2, 13690 },
                                         { 3, 17170 },
                                         { 4, 20650 },
                                         { 5, 24130 },
                                         { 6, 27610 },
                                         { 7, 31090 },
                                         { 8, 34570 }

                              };
Just thought about it, there actually is a shorter way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Proverty_Guidelines
{
    int family_size;
    int income;		
};

int main( int argc, char* argv[] )
{
    Proverty_Guidelines Survey[ 8 ] = { { 1, 10210 },
                                        { 2, 13690 },
                                        { 3, 17170 },
                                        { 4, 20650 },
                                        { 5, 24130 },
                                        { 6, 27610 },
                                        { 7, 31090 },
                                        { 8, 34570 } };
    
    return 0;
}


Printing the values to the screen still has to be done one by one though.

Ninja'd :(
Last edited on
I think I prefer the for loop method. It's more friendly to change. For example, what if the size 1 income jumped by 1000 and all subsequent incomes needed to do the same? That's a lot of editing.

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
#include <iostream>

struct Poverty_Guidelines
{
   int family_size;
   int income;
};

int main( int argc, char* argv[] )
{
   const int SURVEY_SIZE  = 8;
   const int START_INCOME = 10210;
   const int INCOME_STEP  = 3480;

   Poverty_Guidelines survey[SURVEY_SIZE];

   for( int i=0; i < SURVEY_SIZE; ++i )
   {
     survey[i].family_size = i+1;
     survey[i].income = START_INCOME + ( i * INCOME_STEP );

     std::cout << "Family: " << survey[i].family_size;
     std::cout << "\t Income: " << survey[i].income << std::endl;
   }
}
Last edited on
Topic archived. No new replies allowed.