Reading into 3 arrays

Hey guys. I'm trying to write a function that opens a file called "scores.txt" that has the format as follows

exam1 score for student1
exam2 score for student1
exam3 score for student1
exam1 score for student2
exam2 score for student2
exam3 score for student2
exam1 score for student3

My professor started us off with something, but I think he did too much. I can't understand what he is doing as far as reading into the arrays goes, and therefore I don't know how to complete it. Can anybody help out?

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
int buildArrays(float exam1Array[], float exam2Array[], float exam3Array[])
{
ifstream inFile;
int count = 0;
	
inFile.open("scores.txt");
if (inFile.fail())
{
cout << "Unable to open scores.txt file\n";
exit(1);
}

inFile >> exam1Array[count];
while (!inFile.eof())
{
			   
/*read Exam 2 and Exam 3 scores for this student into their respective arrays*/
count++;

inFile >> exam1Array[count];
}
	
	     
inFile.close();
   
return count;
}


And why, in the example, he called it like this?

size = buildArrays(scores);
Last edited on
He called it that way because the function returns the count variable, which is keeping track of how many scores have been read in (and iterating the index up as well).

Essentially all he wants you to do is duplicate his code for the other two arrays. I'll attempt to explain what he's doing.

1
2
ifstream inFile;
int count = 0;

Here he declares a stream that he will use to process the files and an integer counter that he initializes to 0.

1
2
3
4
5
6
inFile.open("scores.txt");
if (inFile.fail())
{
cout << "Unable to open scores.txt file\n";
exit(1);
}

Here he opens the file and checks to make sure it opens correctly (the if statement with infile.fail() condition does that)

1
2
3
4
5
6
7
8
9
inFile >> exam1Array[count];
while (!inFile.eof())
{
			   
/*read Exam 2 and Exam 3 scores for this student into their respective arrays*/
count++;

inFile >> exam1Array[count];
}

This is the real meat of the function. He usese the >> operator to pull in the first score from the file (infile) and then starts a loop that pulls in each subsequent score by increasing the index on the array (count is being used as the index). This loop will continue until the end of the file (or eof, hence the infile.eof() condition in the while). Since infile.eof() returns true when the end has been reached (and the loop must be true to continue running), he uses the '!' symbol to keep running the loop until eof triggers.

1
2
3
inFile.close();
   
return count;

He then closes the stream and returns count (which is the value that gets stored in size when he calls the function as you showed in the example.

The one thing I'm not sure about is why only one parameter is being passed to the buildArrays function that you showed. Was the example for the same function or a different one that only took one array?
Thanks a ton jpeg. That totally makes sense now.

I'm sorry I was using his code to see if what I was working with was wrong. His example only had one array being passed. I'm not sure how I can pass it 3 parameters though, if they're all in the scores.txt file.

How can I make mine only give every 3rd number read in to the exam1Array and the proper numbers to the exam2Array, and ... to the exam3Array, though?
I'm not sure how I can pass it 3 parameters though, if they're all in the scores.txt file.


You don't pass it the files, if you look at the function, you pass it 3 arrays that store the scores, not the file they're contained in.

How can I make mine only give every 3rd number read in to the exam1Array and the proper numbers to the exam2Array, and ... to the exam3Array, though?


The way the text file is setup you have a very specific format of
test1score
test2score
test3score
then it repeats the same pattern
So just arrange the loading in the loop so that the first load is done into the exam1Array, the second into the exam2Array, and the third into the exam3Array. Since this is the pattern that the text file is in, it will continue to load them in order until the end of the file is reached.
Hah, ok. I was getting some basic stuff confused there.

Thanks a lot. I've got it all correct now I believe.

:)
Last edited on
Topic archived. No new replies allowed.