You are here:

C++/class grade average program

Advertisement


Question
This is what I have so far and cannot get it to work any help is appreciated thanks

Write a program that reads a student's name together with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows: 90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F. 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 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).

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 variables. 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 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

Sample Output:
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=



#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;
clrscr();
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();
}

else cout << "Unable to open file";
getch();
return 0;
}
float calculateAverage(int marks[])
{
int tot;
float avg;
for(int i=1;i<=5;i++)
{
tot=tot+marks[i];
}
avg=tot/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;
}

Sorry when I build it this is the errors that I get also having troubles figuring out how to put the calculateGrade function in. Thanks

1>c:\users\the smith's\documents\visual studio 2008\projects\651616103\651616103\5151.c... : error C3861: 'clrscr': identifier not found
1>c:\users\the smith's\documents\visual studio 2008\projects\651616103\651616103\5151.c... : error C3861: 'getch': identifier not found
2 hours ago


Answer
clrscr and getch are the console I/O functions.  Typically those are defined in conio.h.  #include <conio.h> to remove those errors.

I don't think you want to read the whole line at once - otherwise you need to add string parsing to pick off the scores.

Try something like:

input >> name >> grade[0] >> grade[1] and so on to read the line, but store the name and scores as part of the input.

You want to get in the habit of counting from 0.  It's more common in C and C++ to do so.  For 5 grades, count from 0 to 4 (use index < max, or index < 5 in your case).

Hope this helps you to continue.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.