Using a .txt file to fill 2 arrays

I need help making 2 arrays from a single text file. The contents in the array will be side by side. I can only use arrays to do this as well. These are the prompts my professor gave me if that was not clear:

The main program will send n and the two arrays as parameters to the function readdata; readdata will read in n and then read n items into two arrays (which the main program will call score1 and score2). All data values must be read from a file, and all values—including n--must be read by the function.

AThe function readdata will read all the data into two arrays. The function will receive three parameters: an integer which it calls n, and two arrays which it calls nums1 and nums2. At the beginning, none of the parameters will have values. The value of n will be used to control the action of the function, as described below. The function will read in n and then read in n sets of data. Each set of data should contain two integers; the first will be read into nums1, and the second will be read into nums2. (The values stored in these parameters will remain when the function returns to the main program, and they can be used throughout theprogram.) For example, suppose the data set is the following:
3
18 13
21 42
54 66
the readdata function will read the value 3 into the variable n. Then it will read the values 18, 21, and 54 into the array nums1 and the values 13, 42, 66 into the array nums2. Note that you cannot read down the column of values–the two values you read will come from the same line, and you will place the values into the two arrays.

This is the textfile I am using:

5

17 10
12 6
15 7
14 8
19 11

17, 10, 12, 6, and 15 are read into the first array, and the rest are read into the second. I want to first column to be read into the first array, and the second column in the second.

This is what I have so far:

#include <iostream>
#include <fstream>
using namespace std;
void readdata(int&, int[], int[]);
ifstream infile("input5.txt");
ofstream outfile("output6.txt");
int main()
{
int n, score1[n], score2[n];

readdata(n, score1, score2);

return 0;
}

void readdata(int &n, int nums1[], int nums2[])
{
infile >> n;

for(int count = 0; count < n; count++){
infile >> nums1[count];
}
for(int count = 0; count < n; count++){
infile >> nums2[count];
}
return;
}

Last edited on
Topic archived. No new replies allowed.