Hi, i have a problem with this code, which is it only takes the last scores entered by the user when i want to take all the scores of each student and do the avrage calculation and display them like the output ill post with the question.
Hi, i have a problem with this code, which is it only takes the last scores entered by the user when i want to take all the scores of each student and do the avrage calculation and display them like the output ill post with the question.
The code:
#include <iostream>
#include <string>
using namespace std;
void readStudentInfo (string [],int,double[]);
void calcAvg (double[],int);
char getGrade (char[],int, double);
void printResults(string[],double[],double[],int);
void line()
{
cout << "--------------------------------";
}
const int N=2; // Number of students
const int S=5; // Number of tests
int main()
{
string StName[N];
double StScore[S];
printResults(StName,StScore,StScore,N);
}
void readStudentInfo(string names[],int students,double Average[],double Score[])
{
for (int i=0; i<students;i++)
{
cout << "Please enter student name (" << i+1 << "): ";
getline(cin, names[i]);
cout << "\n";
}
for (int j=0; j<students;j++)
{
cout << "Enter all " << S << " scores for " << names[j] << " : " << "\n";
for (int x=0; x<S; x++)
{
cin >> Score[x];
Average[x]+=Score[x];
}
}
}
void calcAvg (string names[], double Average[],int students,double Score[])
{
double StSum=0;
for (int i=0; i<students;i++)
{
StSum=0;
for (int j=0; j<S*N; j++)
{
StSum+=Score[j];
}
cout <<" StSum: " << StSum << endl;
Average[i] = StSum/S;
cout << "AVERAGE IS: " << Average[i] << endl;
}
}
void printResults(string names[],double Score[],double Average[],int students)
{
readStudentInfo(names,students,Average, Score);
double classAvg=0,scoreSum=0;
char grade[N],tempGrade[1],ClassGrade;
double tempAvg=0;
calcAvg(names,Score,students, Score);
for (int x=0;x<students;x++)
{
tempAvg = Average[x];
grade[x] = getGrade(grade,students,tempAvg);
classAvg+= Average[x];
}
classAvg= classAvg/N;
ClassGrade = getGrade(tempGrade,students,classAvg);
cout << "\n";
line ();
cout << "\n";
cout << "Class Average: " << classAvg << endl;
cout << "Class Grade : " << ClassGrade << endl;
line ();
cout << "\n";
for (int i=0; i<students; i++)
{
cout << " Name: " << names[i] << endl;
cout << " Average: " << Average[i] << endl;
cout << " Grade: " << grade[i] << endl;
line ();
cout << "\n";
}
}
char getGrade (char grade[],int students, double Avg)
{
if (Avg>=90)
return 'A';
if (Avg>=80)
return 'B';
if (Avg>=70)
return 'C';
if (Avg>=60)
return 'D';
if (Avg<60)
return 'F';
}
Step by step
Solved in 3 steps with 2 images