Read from txt file to array



I have two files one with states, and one with numbers.
First file:
Alaska
Alabama
New York
Oklahoma
...

Second file:
293 292 403 101
202 102 202 101
203 495 302 959
483 482 495 192
.....

I need the ouput in this format using two-dim array:

Alaska 293 292 403 101
Alabama 202 102 202 101
New York 203 495 302 959
Oklahoma 483 482 495 192

Also, If there are states like New York, New Jersey, it always goes to the next line after "New". How can I have it read the file until the line completely ends?

This is what I have:

void getStateFile(){
ifstream statefile("proj1_states.txt");

if(statefile.is_open()){

for(int i=0; i< NUMROWS; i++){
statefile>>states[i]; //
}
}

}


void getDataFile(){

ifstream datafile("proj1_data.txt");
if(datafile.is_open()){
for(int i=0; i< NUMROWS; i++){
for(int j=0; j<NUMCOL; j++){
datafile>>data[i][j];
}
}
}


void printArrays(string arr[], int arrd[][5], int rowLength, int colLength){
for(int i=0; i<NUMROWS; i++){
states[i]=arr[i];

}

for(int i=0; i<rowLength; i++){
cout<<states[i]<<setw(8)<<"\n";
for(int j=1; j <colLength; j++){
data[i][j] = arrd[i][j];
cout<<setw(8)<<data[i][j]<<" ";
}

cout<<""<<endl;
}

}

My output now looks like this:
Alaska
293 292 403 101
Alabama
202 102 202 101
....so on..


The >> reads only one word. For more. you must getline:
http://www.cplusplus.com/reference/string/string/getline/

You have a newline (\n) in your output between name of state and the numbers.
Topic archived. No new replies allowed.