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.
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 :)
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 :).
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