You probably shouldn't input the
a
and
b
values inside
finalgrade()
. Those values should be read from the user inside
main()
and then passed to
finalgrade()
:
1 2 3 4 5 6
|
float finalgrade(float a,float b)
{
float result;
result=(a*b)/100.00; // ?? The math seems odd.
return result;
}
|
Beware of creating variable-length arrays in C++. It isn't supported by the standard, although some compilers will allow it. Consider using
vector<>
instead.
That said, namesurname always has two members in the last index (the name and surname) and reggrade should only have 2 also (the registry number and result of calling finalgrade():
1 2
|
string namesurname[n][2]; //name and surname
int reggrade[n][2]; //registry number and grade
|
As an aside, it would make more sense to store this stuff in a struct, but I understand you're doing this as practice for a2D array.
Now let's think about the input phase. For each pass through the
for
loop, you want the user to see:
enter name and then surname: <name> <surname>
enter registry number and grades to enter in the function: <registry number> <a> <b> |
The key here is there is no inner loop:
1 2 3 4 5 6 7 8 9
|
for(i = 0; i < n; i++) {
cout<<"enter name and then surname: ";
cin>>namesurname[i][0] >> namesurname[i][1];
cout<<"enter registry number and grades to enter in the function: ";
int a, b;
cin >> reggrade[i][0] >> a >> b;
reggrade[i][1] = finalgrade(a, b); // Compute and store the final grade.
}
|
Now the output. For each person, it appears that you want to print:
name surname registryNumber finalgrade |
So:
1 2 3 4
|
for(i = 0; i < n; i++) {
cout << namesurname[i][0] << ' ' << namesurname[i][1]
<< ' ' << reggrade[i][0] << ' ' << reggrade[i][1] << '\n';
}
|
When I put this all together, I get this run:
arithmos mathiton= 3
enter name and then surname: name1 surname1
enter registry number and grades to enter in the function: 1 20 30
enter name and then surname: name2 surname2
enter registry number and grades to enter in the function: 2 30 30
enter name and then surname: name3 surname3
enter registry number and grades to enter in the function: 3 40 30
name1 surname1 1 6
name2 surname2 2 9
name3 surname3 3 12 |