Reputation: 13
I am trying to make a program that receives from the user 4 student names and 3 test scores for each student. I have a problem when it comes to displaying every name with its 3 test scores. For example if I have John, Bob, Michael, and Mary, all 4 student display along with Mary's 3 test scores.
Here is the function that I have for getting the data from the user:
void Student::getName()
{
for (int i = 0; i < 4; ++i)
{
cout << "Enter name: ";
cin >> name[i];
for (int j = 0; j < 3; j++)
{
cout << "Enter grade: ";
cin >> testScores[j];
}
}
}
And this is the function that I have for displaying the data:
void Student::setName()
{
for (int i = 0; i < 4; ++i)
{
cout << "\nName: " << name[i] << endl;
for (int j =0; j <3; j++)
{
cout << "Grade: " << testScores[j] << endl;
}
}
}
The first function works fine but I have problem with the output of the second function. Any suggestions? Thank you.
Upvotes: 0
Views: 98
Reputation: 10184
Make testScores
a 2D array instead of a single dimension array otherwise you will overwrite student 1's grades with those of student 2 which in turn will be overwritten by those of student 3 and so on leaving you with just the grades of student 4.
int testScores[4][3];
Then
replace
cin >> testScores[j];
with
cin >> testScores[i][j];
in the first function.
And
replace
cout << "Grade: " << testScores[j] << endl;
with
cout << "Grade: " << testScores[i][j] << endl;
Upvotes: 1