Read data from file to 2D array

Feb 20, 2017 at 9:49pm
Hello everyone, I am having trouble with an assignment where I have to read in letter grades from a file and then display them on screen.
My thought process was to use two for loops in order to cycle through the file and then display it but all I am getting so far is this: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

All the text file has is this:
A
A
B
C
C
F
C
D
B
B
A
C
B
A
B

I would really appreciate it if anyone can give me a hint about what I'm doing wrong. Thank you for your time.

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

int main()
{
	ifstream inputFile;
	const int ROW = 4;
	const int COL = 4;
	char student[ROW][COL];

	inputFile.open("grades.txt");

	if (inputFile)
	{
		for (int i = 0; i < ROW; i++)
		{
			for (int j = 0; j < COL; j++)
				cout << student[i][j];
		}
		cout << endl;
	}
	else
	{
		cout << "Error opening the file.\n";
	}

	inputFile.close();

	return 0;
}
Last edited on Feb 20, 2017 at 9:51pm
Feb 20, 2017 at 10:02pm
I would really appreciate it if anyone can give me a hint about what I'm doing wrong.

Where are you attempting to actually read the file contents?

Feb 20, 2017 at 10:32pm
Hi, I modified it so that it looks like this now:
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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inputFile;
	const int ROW = 4;
	const int COL = 4;
	char student[ROW][COL];

	inputFile.open("grades.txt");

	if (inputFile)
	{
		inputFile >> student[ROW][COL];

		for (int i = 0; i < ROW; i++)
		{
			for (int j = 0; j < COL; j++)
				cout << student[ROW][COL] << endl;
		}
		cout << endl;
	}
	else
	{
		cout << "Error opening the file.\n";
	}

	inputFile.close();

	return 0;
}

But now it is just outputting 1's.
I'm really sorry if this is a dumb question but this is the first time I'm working with 2D arrays.
Feb 21, 2017 at 3:34am
You may want to review your documentation on arrays. And it might be easier if you start with a statically allocated and populated array, to get your print function working correctly.

Something like:

1
2
3
4
5
6
7
int main()
{
    int array[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

    // Now print out the above array to the console (cout).

...



A tutorial on arrays: http://www.cplusplus.com/doc/tutorial/arrays/

By the way the code in the OP is closer than your "new" code.
Last edited on Feb 21, 2017 at 3:34am
Feb 21, 2017 at 6:55pm
If I've understood you correctly, this is easier than you think. You can replace 15 by the number of grades you want (that is, the number of grades in your grades file).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	ifstream in ("grades.txt");
	char grade;

	for (int i=0;i<15;i++) 
	{
		in >> grade; // grade equals each one of the grades, but only one at a time.
		cout << grade << endl; // this line displays each grade
	}
}
Feb 27, 2017 at 7:16pm
Thank you both for your help :)
Topic archived. No new replies allowed.