averages of my arrays are not right and letter grades are not showing up

Write a program that uses an array of string objects to hold the five student names, an array of five characters to hold the five students’ letter grades, and five arrays of four double s to hold each student’s set of test scores.
The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average. Input Validation: Do not accept test scores less than 0 or greater than 100.


#include <iostream>
#include <string>
using namespace std;

int main()
{
int i;

float sum=0;
string name[5];
float avg[5];
char grade[5];
double testscore1[4];
double testscore2[4];
double testscore3[4];
double testscore4[4];
double testscore5[4];




{
cout<<"Enter name for student 1 \n";
cin>>name[0];

cout<<"Enter scores for student 1\n";
for(int i=0; i<4; i++){
cin>>testscore1[i];
sum=sum+testscore1[i];}

avg[i]=(sum/4);
}
{
cout<<"Enter name for student 2 \n";
cin>>name[1];

cout<<"Enter scores for student 2\n";
for(int i=0; i<4; i++){
cin>>testscore2[i];
sum=sum+testscore2[i];}

avg[i]=(sum/4);
}
{
cout<<"Enter name for student 3 \n";
cin>>name[2];

cout<<"Enter scores for student 3\n";
for(int i=0; i<4; i++){
cin>>testscore3[i];
sum=sum+testscore3[i];}

avg[i]=(sum/4);
}
{
cout<<"Enter name for student 4 \n";
cin>>name[3];

cout<<"Enter scores for student 4\n";
for(int i=0; i<4; i++){
cin>>testscore4[i];
sum=sum+testscore4[i];}

avg[i]=(sum/4);
}
{
cout<<"Enter name for student 5 \n";
cin>>name[4];

cout<<"Enter scores for student 5\n";
for(int i=0; i<4; i++){
cin>>testscore5[i];
sum=sum+testscore5[i];}

avg[i]=(sum/4);

}
if(avg[i]<=100 && avg[i]>=90)
grade[i]='A';
else
if(avg[i]<=89 && avg[i]>=80)
grade[i]='B';
else
if(avg[i]<=79 && avg[i]>=70)
grade[i]='C';
else
if(avg[i]<=69 && avg[i]>=60)
grade[i]='D';
else
grade[i]='F';


for(int i=0;i<5;i++){
cout << "Name: " << name[i]
<<" Average: " << avg[i]
<<" Grade: " << grade[i]
<< endl;
}

return 0;
}


output is
ame: red Average: 405.75 Grade: F
Name: blue Average: 0 Grade:
Name: green Average: -5.48688e-12 Grade:
Name: pink Average: 4.59135e-41 Grade:
Name: Lee Average: -5.48687e-12 Grade:




closed account (iGLbpfjN)
There is no point in using avg[i]=(sum/4);. Instead of this, for student 1 use avg[0]=(sum/4);, for student 2 use avg[1]=(sum/4); and so on.
Also, you have to clear the value of sum after each average, so put sum = 0; after average.
In the grades, you have to specify which average you are working with. So put a for(i = 0; i < 5; i++) before the letter grades.

P.S.: In for loops, you don't need to put int i, you already did that in the beginning. Also, prefer using comments instead of braces to delimit the code parts.
Last edited on
Thank you!!
Topic archived. No new replies allowed.