files struct array

i have an array and struct i read the first file fine the second file im having a problem with because all the id are not in order so how would i compare two struct with '==' and match the id together because everytime i do this it doesnt print anything because im completely lost
please help\\\
Last edited on
Please provide code for your problem. It sounds like a mix of getline and operator>>

See:
http://en.cppreference.com/w/cpp/string/basic_string/getline

especially the Notes section on how to solve this problem.
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
struct student
{
   int studentNum;
   string name;
   string letterGrade;

};
struct gPa
{
    double gradePoint;
    string className;

};
int main()
{
    int i=0;
    int num =0;
    student studentInfo[11];
    ifstream inFile;
    inFile.open("student.txt");
    while(!inFile)
    {
        cout << "error" << endl;
        return 0;
    }



    for(i=0; i>11; i++)
        {
           inFile >> studentInfo[i].studentNum;
           inFile >> studentInfo[i].name;
           inFile >> studentInfo[i].letterGrade;
           cout << studentInfo[i].studentNum << studentInfo[i].name << studentInfo[i].letterGrade << endl;

    }

    inFile.close();
    inFile.open("gpa.txt")


text file student:
0 billy A
1 matt A
2 mike B
3 john C
4 kat D
5 cindy C
6 vera A
7 tom B
8 bob D
9 dave C
10 ant A
text file gpa:
10 4 Math
2 3 Math
3 2 Science
7 3 history
8 1 english
9 2 English
5 2 science
6 4 Math
4 1 English
1 4 history
0 4 history

so i dont get is how you would match files from a struct and array so that the 10 match with 10 element would you compare structs "==" or would it be something different. i can use void function also
Last edited on
please any feedback

i want to understand it but dont have any idea
@ghost1111
A couple of problems. You had for loops wrong. It would be
for(i=0 ; i < 11 ; i++).
Not >, which is greater than.
Anyway, here's how to fill the structs, and cout them in the order of the student number, so they all match up.

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

using namespace std;

struct student
{
	int studentNum;
	string name;
	string letterGrade;

};

struct gPa
{
	int studentNum; // You never added this one to yours
	double gradePoint;
	string className;

};
int main()
{
	int i=0, j=0;
	int num = 0;
	student studentInfo[11];
	gPa student_GPA_Info[11];
	ifstream inFile;
	inFile.open("student.txt");
	if(!inFile)
	{
		cout << "Could not find the file. Error" << endl;
		return 0;
	}
	for(i=0; i < 11; i++)
	{
		inFile >> studentInfo[i].studentNum;
		inFile >> studentInfo[i].name;
		inFile >> studentInfo[i].letterGrade;
	}
	inFile.close();

	ifstream GPA_File;
	GPA_File.open("gpa.txt");// Must open with different file name
	if(!GPA_File)
	{
		cout << "Could not find the file. Error" << endl;
		return 0;
	}
	for(i=0; i < 11; i++)
	{
		GPA_File >> student_GPA_Info[i].studentNum;
		GPA_File >> student_GPA_Info[i].gradePoint;
		GPA_File >> student_GPA_Info[i].className;
	}
	GPA_File.close();
	for(i=0; i < 11; i++)
	{
		cout << studentInfo[i].studentNum << ") " << studentInfo[i].name << " " << studentInfo[i].letterGrade << " ";
		for( j=0; j < 11; j++)
		{
			if (studentInfo[i].studentNum == student_GPA_Info[j].studentNum)
				cout << student_GPA_Info[j].gradePoint << " " << student_GPA_Info[j].className << endl;
		}
		cout << endl;
	}
	cin >> num;
return 0;

}
Last edited on
i see now ok and thank you now i understand
but why the int num
wouldnt they all just line up with each other
@ghost1111

You had int num at the start of your source, so I just used it, so I could keep the console open after the program runs.

wouldnt they all just line up with each other

What are you meaning here?

If you're referring to studentInfo and student_GPA_Info, then, no. The second set, student_GPA_Info, is not in the same order as studentInfo, so we do a loop to find the same student number in each set, and print it out on the same line. Then print a new line or, endl.

If you want consecutive lines, with no space between then, remove the '<< endl' on line 64, but leave the semi-colon
Topic archived. No new replies allowed.