Hi, I'm working on a c++ homework assignment and I cannot get it to work.
Here is the assignment:
Write a program that reads a student's name together with his or her test scores. The program shouuld then computer the average test score for each student and assign the appropriate grade. The grade scale is as follows: A = 90-100 B = 80-89 C = 70-79 D=60-69 F = 0-59 Your program must use the following functions:
a. A void function, calculateAverage, to determine the average of the five test scores for each student. Use a loop to read and sum the five test scores. (This function does not output the average test score. That task must be done in the function main.)
b. A value returning function, calculateGrade, to determine and return each student's grade. (This function does not output the grade. That task must be done in the function main. )
Test your program on the following data. Read the data from a file and send the output to a file. Do not use any global variable. Use the appropriate parameters to pass values in and out of functions.
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
#include <iostream>
#include <fstream>
using namespace std;
char calculateGrade(double);
void calculateAverage(ifstream&,int[],int,double&);
int main(void)
{
string name ;
int i,numgrades=5,grade[5];
double average;
char letter;
ifstream in;
ofstream out;
in.open("inData.txt");
if(in.fail())
{
cout << "input file did not open please check it\n";
system("pause");
return 1;
}
out.open("testavg.txt"); //open file
if(out.fail()) //is it ok?
{ cout<<"output file did not open please check it\n";
system("pause");
return 1;
}
out<<"Student\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade\n";
in>>name;
while(in)
{calculateAverage(in,grade,numgrades,average);
letter= calculateGrade(average);
out<<name<<"\t";
for(i=0;i<numgrades;i++)
out<<grade[i]<<"\t";
out<<average<<"\t"<<letter<<"\n";
in>>name;
}
in.close();
out.close();
system("pause");
return 0;
}
char calculateGrade(double average)
{if(average >=90)
return 'A';
else
if(average >=80)
return 'B';
else
if(average >=70)
return 'C';
else
if(average >=60)
return 'D';
else
return 'F';
}
void calculateAverage(ifstream& in,int grade[],int max,double& average)
{int i,sum=0;
for(i=0;i<max;i++)
{in>>grade[i];
sum+=grade[i];
}
average=(double)sum/max;
}