Arrays of type struct.

It gives me error in the initialization of array along with lot more errors.I tried a lot to find the reason of errors but could not.Please compile it in your compiler and tell me hw can I remove the errors.Also please tell me the reasons of errors
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{	
	struct data
	{
		string country;
		string fathername;
		int   age;
	};
		data NAME;

	data name[5]={"Ali", "Usman", "Sarmad", "Awais", "Junaid"};
	name[0]=(struct data){"Saudi Arabia","Tahir Awan",24};
	name[1]=(struct data){"England","Akram Khan",20};
	name[2]=(struct data){"China","Amjad Ali",20};
	name[3]=(struct data){"Syria","Ahmad Ali",19};
	name[4]=(struct data){"Oman","Zohaib Sultan",24};
	
			cout << "Enter Name of student/n";
			cin >>  NAME;
	
	int j;
	for(int i=0; i<6; i++)
	{
		if(NAME==name[i])
		j=i;
	}
	cout << "We have following data for   " << name[j]<< endl;
	cout << "Country:  " << name[j].country << endl << "Father name:  " << name[j].fathername << endl << "Age:  " << name[j].age << endl;
	
	return 0;
	
}
Last edited on
To create and initialize your array of data you need to initialize all elements in the same statement. Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct data
{
   string country;
   string fathername;
   int   age;
};

int main()
{
   data name[] { {"Saudi Arabia","Tahir Awan",24}, {"England","Akram Khan",20}, {"China","Amjad Ali",20},
                 {"Syria","Ahmad Ali",19}, {"Oman","Zohaib Sultan",24}};


When you create and initialize an array of elements, you must use the same data of your array-type. Like:

int A[5] = { 1, 2, 3, 4, 5 };

Here A is an integer array and for that five integers are used to initialize it.
The compiler reads the integers from left to right and assigns the values to A[0] to A[4] sequentially. Therefore, the single statement is equivalent to the following five statements:
1
2
3
4
5
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 4;
A[4] = 5;


But, in your code you tried to initialize an array, which is of type data, using strings!
You have to provide all the three data from left to right matching the data-fields of your structure to initialize every element of that array. Like this:
1
2
data name[5] = { "Saudi Arabia","Tahir Awan",24, "England","Akram Khan",20, "China","Amjad Ali",20,
    "Syria","Ahmad Ali",19, "Oman","Zohaib Sultan",24};

Five sets of data will initialize your array from index 0 to 4 like this:

1
2
3
4
name[0].country = "Saudi Arabia";
name[0].fathername = "Tahir Awan";
name[0].age = 24;
// ....... 

and so on......
Or you can separate them using {}:

1
2
data name[5] { {"Saudi Arabia","Tahir Awan",24}, {"England","Akram Khan",20}, {"China","Amjad Ali",20},
                 {"Syria","Ahmad Ali",19}, {"Oman","Zohaib Sultan",24}};

Last edited on
thanks @jlb and PSYCHAMERON
Topic archived. No new replies allowed.