Parallel Array and an Input Text File

I have a text file for my input and I need to store this information into two arrays. The data in the file is organized in columns of "ID's" and "Wages" like so.

1234 9.75
4567 10.50
8912 7.25

I m trying to store the data into two array's in the same loop.

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<cstring>
#include<fstream>

using namespace std;

int main()
{
    char file[255];

    const int MAX=20;

    int data,cnt;

    //arrays initialized to zero
    //to easily track changes
    int id[MAX]={0};
    float payRate[MAX]={0.00};

    ifstream inFile;

    //user input into cstring "file"
    cout<<"Please enter the file name: ";
    cin>>file; inFile.open(file);

    //gather input from file and store in array
    for(cnt=0;cnt<MAX;cnt++)
        inFile>>id[cnt]>>payRate[cnt];

    //display the array elements
    for(cnt=0;cnt<MAX;cnt++)
        cout<<id[cnt]<<" "<<payRate[cnt];
}

So far I can only input and display "1234" and 9.75.

How would I iterate through the rest of the data?

Thanks
cw

Last edited on
Okay, so I've made a rookie mistake. I've gone back and simplified some stuff and found that I left off the stream manipulator in my output loop in line 32. Because I found this little error I have confirmed my program does read in my data from the file into the right arrays from what I can tell.

Anyway, I haven't figured out how to make the for loop in line 27 to quit when it comes to the end of the data in the file.
Topic archived. No new replies allowed.