Infiling into an Array

Hello, I'm still pretty new to C++. I'm wondering if anyone can guide me in the right direction? For homework, I'm supposed to infile the following from this text file into an array:

9th
10th
11th
12th
F
C
B
A
Math
21
15
32
14
5
8
41
22
9
11
32
21
4
25
21
4
Science
17
5
10
16
25
14
19
24
26
7
15
13
14
16
5
5
English
7
12
15
16
22
21
16
9
9
14
16
12
12
18
8
34


The output should look similar to this (The x's should be the numbers):

MATH: F C B A
9th x x x x
10th x x x x
11th x x x x
12th x x x x

SCIENCE: F C B A
9th x x x x
10th x x x x
11th x x x x
12th x x x x

ENGLISH: F C B A
9th x x x x
10th x x x x
11th x x x x
12th x x x x



This is my code so far:


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



int main()
{
	ifstream inFile;
	inFile.open("Test_Scores.txt");

	string year[4];		//there are 4 years 9th - 12th
	char letter[4];    // there are 4 letters, A-F
	string subject[3];		//only 1 subject
	int grades[3][4][4];  // there are 48 grades


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

		inFile>>subject[z]; // since there is only 1 subject.


		for (int x=0; x<4; x++) // getting the 4 letters.
		{
			inFile>>letter[x];
		}



		for (int x=0; x<4; x++)		//the next 4 lines look the same
		{
			inFile>>year[x];		//it starts with a year

			for (int y=0; y<4; y++)
			{
				inFile>>grades[z][x][y]; // and gets 4 grades for that row
			}
		}

	}


	////////////////////////////////////////////////////// Display


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

		cout<<subject[z]; 


		for (int x=0; x<4; x++) 
		{
			cout<<letter[x];
		}


		for (int x=0; x<4; x++)		
		{
			cout<<year[x];		

			for (int y=0; y<4; y++)
			{
				cout<<grades[z][x][y];
			}
		}

	}



	return 0;
}



Pretty simple, I know, but I just can't seem to figure it out. I've been working on this for about five hours trying different approaches but keep getting weird numbers when I run it like "-858993460" or symbols "||}". Can someone explain what I'm doing wrong? I know it's something so simple that I'm oblivious to.
Last edited on
Your sample data has:
4 year names
4 letters
3 subjects

where each subject has:
1 name
4*4 numbers


Your current code reads 3 times:
a name
4 letters
4 * ( year + 4 numbers )


Do you now see the mismatch?
Thank you!
Topic archived. No new replies allowed.