.txt into 2d array help?

I need to read this .txt file into an array;

John 100 100 90 95 85 100 90 100 100 98 75
Tom 90 92 82 95 89 93 95 97 96 98 92
Shannon 100 90 95 85 100 65 75 95 100 90 60
Matt 98 90 95 85 100 90 90 95 100 90 82
Steve 100 100 90 95 85 75 85 90 95 100 78
Nicole 90 90 95 100 75 100 100 90 92 82 68

The disaster I've managed to create so far;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

ifstream d_in;
ofstream d_out;
d_in.open("Score.txt");

if (d_in.fail())
{
cout << "Input file failed to open." << endl;
exit(1);
}

int scores [6][12];
d_in >> scores;

return 0;
}

Gives me this error;
error: no match for 'operator>>' in 'd_in >> scores'

I've search the internet and my book for hours and I am completely lost.
Last edited on
Changed

d_in >> scores;

to

for (x=0;x<6;x++)
for (y=0;y<12;y++)
{
d_in >> scores[x][y];
}

and the error went away, but the array is still just filled with random garbage and not the info from the file.
I can see two different arrays in this file, which are parallel. There is a one-dimensional array of type string and a two-dimensional array of type int.

You can declare both arrays like the following:
1
2
string name[6]; //array to store the names
int score[6][11]; //array to store the scores 


Notice that the score array is 6 x 11, not 6 x 12 because the names are not part of that array (remember arrays can only hold data that are of the same type. Since you are inputting strings into an array of type int, you are getting garbage.
Before you process the scores of say, the first person, make sure you read the person's name first. Just to push you in the right direction, since you are going to be processing two arrays, replace
1
2
3
4
5
for (x=0;x<6;x++)
for (y=0;y<12;y++)
{
d_in >> scores[x][y];
}

with
1
2
3
4
5
6
for(int x = 0; x < 6; x++)
{
  //Fill this in
  for(int y = 0; y < 11; y++)
  //Fill this in
}

Keep in mind the first row ( x = 0 ) corresponds to the first person in the string array.
Last edited on
so i would fill it in with


for (x=0;x<6;x++)
{
  d_in >> name [x];

  for (y=0;y<12;y++)
  d_in >> scores [6][y];
}


Is that correct? Im confused.
You almost got it. Remember, since you have
int score[6][11];
the indices of the 'row' range from 0 to 5 and the indices of the 'columns' range from 0 to 10.

So, your index for the row is out of bounds:
1
2
3
4
5
6
7
for (x=0;x<6;x++)
{
  d_in >> name [x];

  for (y=0;y<12;y++) //Change y < 12 to y < 11
  d_in >> scores [6][y]; //error
}


Also, if you are simply placing a constant as one of the indices, you will always be referring to the same row (or column), which you don't want. If you think you got it, make sure to post your code :)
Last edited on
Now you just need this part:
1
2
3
4
5
6
7
for(int x = 0; x < 6; x++)
{
  d_in >> name[x];

  for(int y = 0; y < 11; y++)
  //Fill this in
}
so when i do the d_in part it needs to be the actual row number?

d_in >> scores [5] [y];
Ok, that is a valid statement now. However, think about it, that statement is only referring to the 6th row. You have to process the first five as well. In other words, you shouldn't have a constant (an integer) in the row subscript. What would happen if you replaced 5 with x? Post your code so I can make sure you've got everything :).
Last edited on
so this?

for(int x = 0; x < 6; x++)
{
d_in >> name[x];

for(int y = 0; y < 11; y++)

d_in >> scores[x][y];
}

how does that process eliminate the problem?
holy s! it works! finally after 2 days! thank you so much
Yes, that is correct. If you replace the d_in >> with cout << you should see the results.

how does that process eliminate the problem?
Because now you are reading all the rows. When you had
d_in >> scores[5][y];
you were only referring to the sixth row and the first five were being ignored.

d_in >> scores[x][y]; (The x comes from the outer for loop which will range from 0 to 5).

Now, the data will be read like this..
for the first row -
1
2
3
4
5
6
7
d_in >> name[0];

d_in >> scores[0][1];
d_in >> scores[0][2];
.
.
d_in >> scores[0][10]; //read the last score 


and the second row -
1
2
3
4
5
6
7
d_in >> name[1];

d_in >> scores[1][1];
d_in >> scores[1][2];
.
.
d_in >> scores[1][10]; //read the last score  


and so on until you've read all sixth rows.

Last edited on
Topic archived. No new replies allowed.