C++/Class Grade Program Help COntinued
Expert: Prince M. Premnath - 4/21/2009
QuestionQUESTION: Thanks again for all your help. Sorry Im asking so many ?s but I missed 2 weeks of class because we had a baby and I am lost. That program works but it just outputs the names on the screen and in the file the names and the grade are shown. I also cannot figure out how to add these as I am so lost right now. Like I said it shows nothing but the names on screen
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 fucntion 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 students's grade. (This fucntion does not output the average test score that task must be done in the function main).
and I need it to look like this
The output should be of the following form: (Fill the last two columns and the alst line showing the class average
Student Test1 Test2 Test3 Test4 Test5 Average Grade
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 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 98
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
Class Average=
Here is the code again thanks again for all your wonderful help
__________________________________________________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
float calculateAverage(int[]);
char Grade(float);
char line[100];
int marks[100];
ifstream input;
ofstream output;
input.open("k:\\input.txt");
output.open("k:\\OUTPUT.txt");
if(!output)
cout<<"Output file error";
if (input)
{
while (!input.eof() )
{
input>>line;
cout << line <<endl;
output<<line;
for(int i=1;i<=5;i++)
{
int num;
input>>num;
marks[i]=num;
}
float avg=calculateAverage(marks);
char grd=Grade(avg);
output<<grd<<endl;
}
input.close();
}
return 0;
}
float calculateAverage(int marks[])
{
int tot = 0;
float avg;
for(int i=1;i<=5;i++)
{
tot=tot+marks[i];
}
avg=tot/(float)5.0;
return avg;
}
char Grade(float avg)
{
char g;
if((avg<=100)&&(avg>=90))
g='A';
else if((avg<=89)&&(avg>=80))
g='B';
else if((avg<=79)&&(avg>=70))
g='C';
else if((avg<=69)&&(avg>=60))
g='D';
else
g='F';
return g;
}
ANSWER: Hi Dear Jerry !
Now check out this programme , it will display the data's from the output file just the way you have mentioned !
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main ()
{
float calculateAverage(int[]);
char Grade(float);
char line[100];
int marks[100];
float TotalAvg = 0.0;
int nStuds = 0;
ifstream input;
ofstream output;
input.open("c:\\m\\tc\\input.txt");
output.open("c:\\m\\tc\\OUTPUT.txt");
if(!output)
cout<<"Output file error";
if (input)
{
while (!input.eof() )
{ ++nStuds;
input>>line;
output<<line;
for(int i=1;i<=5;i++)
{
int num;
input>>num;
marks[i]=num;
output<<num;
}
float avg=calculateAverage(marks);
TotalAvg += avg;
output<<avg;
char grd=Grade(avg);
output<<grd<<endl;
}
}
input.close();
output.close();
input.open("c:\\m\\tc\\OUTPUT.txt");
while(!input.eof())
{
input>>line;
cout<<line<<endl;
}
input.close();
cout<<"Class Average = "<<TotalAvg /(float) nStuds;
return 0;
}
float calculateAverage(int marks[])
{
int tot = 0;
float avg;
for(int i=1;i<=5;i++)
{
tot=tot+marks[i];
}
avg=tot/(float)5.0;
return avg;
}
char Grade(float avg)
{
char g;
if((avg<=100)&&(avg>=90))
g='A';
else if((avg<=89)&&(avg>=80))
g='B';
else if((avg<=79)&&(avg>=70))
g='C';
else if((avg<=69)&&(avg>=60))
g='D';
else
g='F';
return g;
}
Note: This Programme will suffer from alignment issues , i kindly request you to spend some time to rectify those issues , i haven't tested this programme revert me in case of logical bug's
Thanks and Regards!
Prince M. Premnath
---------- FOLLOW-UP ----------
QUESTION: This is what I came up with seems to work pretty good thanks
#include <iostream> // Student Grades Program
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
double calculateAverage(double test1, double test2, double test3, double test4, double test5);
int calculateGrade(double avg);
void main()
{
string studentName;
double test1, test2, test3, test4, test5;
double average=0.0;
char grade;
int studentCount=0;
double sumAverage=0.0;
double cAverage=0.0;
int i;
ifstream inFile;
inFile.open("k:\\input.txt");
ofstream outFile;
outFile.open("k:\\Output.txt");
cout << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Test3"
<< setw(10) << "Test4" << setw(10) << "Test5" << setw(10) << "Average" << setw(10)
<< "Grade" << endl;
outFile << "Student" << setw(10) << "Test1" << setw(10) << "Test2" << setw(10) << "Test3"
<< setw(10) << "Test4" << setw(10) << "Test5" << setw(10) << "Average" << setw(10)
<< "Grade" << endl;
while(inFile.good())
{
inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;
average= calculateAverage(test1, test2, test3, test4, test5);
grade = calculateGrade(average);
i= 17-studentName.size();
cout << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << test3
<< setw(10) << test4 << setw(10) << test5 << setw(10) << average
<< setw(10) << grade <<endl;
outFile << studentName << setw(i) << test1 << setw(10) << test2 << setw(10) << test3
<< setw(10) << test4 << setw(10) << test5 << setw(10) << average
<< setw(10) << grade <<endl;
sumAverage = sumAverage + average;
studentCount++;
}
cAverage= sumAverage / studentCount;
cout << endl << "Class average is:" << cAverage<< endl;
outFile << endl << "Class average is:" << cAverage<< endl;
}
double calculateAverage(double test1, double test2, double test3, double test4, double test5)
{
double avg;
avg = (test1 + test2 + test3 + test4 + test5) / 5;
cout << setprecision(2) << showpoint(2) <<
return avg;
}
int calculateGrade(double avg)
{
char grade;
if (avg<=100 && avg>=90)
grade='A';
else if (avg<90 && avg>=80)
grade='B';
else if (avg<80 && avg>=70)
grade='C';
else if (avg<70 && avg>=60)
grade='D';
else if (avg<60 && avg>=0)
grade='F';
return grade;
}
AnswerHi dear Jerry !
The most effective way of programming this problem is usage of structures or class , usage of structures will simplify your processing needs!
Advantage of employing structure is it will enable you to deal the data as a record and not a individual field which is no way related to each other or we can call its as non-related data.
eg: define a structure as follows
struct stud{
char name[20];
int marks[5];
float average;
char grade;
}record;
populate the structure and write onto the file at once
for record based read / write you should use the read() and write() function call's just try this!
Thanks and Regards !
Prince M. Premnath