Importing from .txt into arrays

"Write a program that calculates the students’ average test scores and their grades. Use three arrays: a one-dimensional array to store the student names, a two-dimensional array to store the test scores, and a one-dimensional array to store the grades. Your program must contain at least the following functions: read and store data into two arrays, a function to calculate average test score and grade, and a function to display the results. Have your program also find and output the class average."

I have been trying to get this program to work for hours and I am no where close. It is really frustrating.

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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	string names[5];

	const int rows = 5;
	const int columns = 5;

	int row, col;
	int i, score1, score2, score3, score4, score5;
	int scores[rows][columns];

    ifstream inFile;
    inFile.open("C:\Users\STUDENTS\Desktop\Ch9Ex12.txt");

	if (!inFile){
        cout << "File not found!" << endl;
    }

    while (inFile){
		inFile >> names[5] >> score1 >> score2 >> score3 >> score4 >> score5;

	}

	for (row = 0; row < rows; row++){
		for (col = 0; col < columns; col++)
			cout << setw(5) << scores[row][col];
		cout << endl;
	}

    inFile.close();

    return 0;

    

} 


I know that I am nowhere close, but a push in the right direction would be very helpful.
Last edited on
I have been trying to get this program to work for hours and I am no where close. It is really frustrating.

If you get stuck for more than 30 minutes, seek help. It's easy to spin your wheels for hours and hours but it isn't productive.

You've got some good stuff here, it just needs to be rearranged a little and expanded on. First, notice that the assignment says you should have these functions. Follow those instructions from the start. It looks like you're trying to write the code first, and then break it into functions later.

Use three arrays: a one-dimensional array to store the student names, a two-dimensional array to store the test scores, and a one-dimensional array to store the grades.


You've got most of this. I suggest:
1
2
3
4
5
6
7
8
9
const int NumStudents = 5;   // at file scope so you can use them everywhere
const int NumTests = 5;

int main()
{
	string names[NumStudents];
	int scores[NumStudents][NumTests];
	string grades[NumStudents];   // this one is new
	...

Note that I've renamed rows and cols to be NumStudents and NumTests because that's more descriptive

Your program must contain at least the following functions: read and store data into two arrays

I guess that should read the names and the test scores:
1
2
3
void read(string names[NumStudents], int scores[NumStudents][NumTests], istream &is)
{
}

Notice that I've passed the stream as a parameter. This makes it more flexible than, say, passing in the name of the file.

a function to calculate average test score and grade

Hmm. Are you supposed to print the average scores? I didn't see this anywhere. Okay, let's make a function that takes one set of test scores and returns a grade
1
2
3
string getGrade(int scores[NumTests])
{
}


And finally the display function:
1
2
3
void display(string names[NumStudents], int scores[NumStudents][NumTests], string grades[NumStudents], ostream &os)
{
}


Now your main function will look like this:
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
int main()
{
	string names[NumStudents];
	int scores[NumStudents][NumTests];
	string grades[NumStudents];   // this one is new

	ifstream inFile;
	// NOTE THE DOUBLE SLASHES BELOW!
	inFile.open("C:\\Users\\STUDENTS\\Desktop\\Ch9Ex12.txt");

	if (!inFile){
        	cout << "File not found!" << endl;
		return 1;
	}
	read(names, scores, inFile);
	inFile.close();

	// Calcuate the grades
	for (student = 0; student < NumStudents; ++student) {
		grades[student] = getGrade(scores[student]);
	}

	// display the results
	display(names, scores, grades, cout);
	return 0;
} 


Hope this helps.
Thanks for the reply, however I am still totally lost. I have no idea how void functions will fit into this program. I have no idea how to go from the text file to the array. I have no idea how to input numbers from a text file into a two dimensional array.
Can you post the text file? You probably just input the data similarly to line 28 of your original post.

A void function is just a function that returns no value. In my example, notice that I don't do anything with the read() and display() calls. That's what void functions are for: doing something and not returning a value.

Note however that in the real world of programming, you almost always return a value - frequently a bool that indicates whether the function succeeded or failed. I'm ignoring failure conditions in this case.
Topic archived. No new replies allowed.