store data in arrays of struct

hello every one .

i want to put this text file data in arrays using struct , but i have some syntax and i didnt understant this , what i should study and can any one help me to solve this



my text file
1
2
3
Sam Albert 18 20162176110 86.7 Math101 CS101 Bio101
Sara Ali 21 20150091030 65.2 CS112 Physics102 CIS240
John Mathio 20 20160175098 70.8 English112 NES201 EE212

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

using namespace std;
string initializes(StudentInfo s[], int x);

struct PersonInfo {

	string FirstName;
	string LastName;
		int Age;

};


struct StudentInfo {

	PersonInfo HumanBeing;
	unsigned long long int UniversityID;
	double GPA;
	string Courses[3];
};



void print() {


}





string initializes (StudentInfo s[], int x)
{


	ifstream i;
	ofstream o;
	i.open("lol.txt");
	while (!i.eof)
	{

		for (int j = 0; j < 3; j++)
		{

			i >> s.HumanBeing[j];

		}


	}
	for (int j = 0; j < 3; j++)
	{

		cout << s[j];

	}


}


int main() {

	ifstream i;
	ofstream o;
	int x = 3;
	StudentInfo sam[3];
	initializes(sam, x);

}
Last edited on
Change line 49:

i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> ... >> s[j].UniversityID >> ...;

Line 58 is wrong. Outputing all members is the same as input just with different operators:

cout << s[j].HumanBeing.FirstName << s[j].HumanBeing.LastName << ... << s[j].UniversityID << ...;
now i have 9 errors

2 in line 6
3 in line 12
3 in line 13
1 in line 52


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

using namespace std;
void initializes(StudentInfo s[], int x);

int main() {
	ifstream i;
	ofstream o;
	int x = 3;
	StudentInfo sam[3];
	initializes(sam, x);

}

struct PersonInfo {

	string FirstName;
	string LastName;
		int Age;

};


struct StudentInfo {

	PersonInfo HumanBeing;
	unsigned long long int UniversityID;
	double GPA;
	string Courses[3];
};



void print() {


}





void initializes (StudentInfo s[], int x)
{


	ifstream i;
	ofstream o;
	i.open("lol.txt");
	while (!i.eof)
	{

		for (int j = 0; j < 3; j++)
		{

			for (int b = 0; b < 3; b++)
			{ 
			i >> s[j].HumanBeing.FirstName >> s[j].HumanBeing.LastName >> s[j].HumanBeing.Age >> s[j].UniversityID >> s[j].GPA >> s[j].Courses[b];
			}
		}


	}
	for (int j = 0; j < 3; j++)
	{

		for (int b = 0; b < 3; b++)
		{
			cout << s[j].HumanBeing.FirstName << s[j].HumanBeing.LastName << s[j].HumanBeing.Age << s[j].UniversityID << s[j].GPA << s[j].Courses[b];
		}
	}

}
move the structs before the first line you actually use it. I.e. line 6.

Line 52: eof is a function hence change it to eof() // Note: ()

Actually you read/write it wrongfully. Move everything from line 60/71 before line 58/69 except for s[j].Courses[b] which remains in the inner loop.
Line 52: Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) 
  {  //  Good operation
  }

Topic archived. No new replies allowed.