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.
#include<iostream>
#include<cstring>
#include<fstream>
usingnamespace std;
int main()
{
char file[255];
constint 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.
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.