You are here:

# C++/Average Class Grade Program C++?

Question
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 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);
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 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 void function in. Thanks

2 hours ago

It sounds like you are not including something in your build. I have provided the following link to give you and idea about clrscr:http://www.computing.net/answers/programming/i-cant-get-clrscr-to-work/3510.html.

For getch check your documentation and see what to include for that.

---------- FOLLOW-UP ----------

QUESTION: Yeah Im a moron was using Turbo C++ and Visual Studio. Here is my new code everything works fine but when I put in Robinson it does not line up. Any ideas? THanks

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#define N 5

char names[N][20];
int winnerPos;

void get_input()
{
int i;
for (i=0;i<N;i++)
{
cout << "Enter Last Name of Candidate" << endl;
cin >> names[i];
}
}

void calculate_winner()
{
int i;
winnerPos = 0;

for (i=1;i<N;i++)
{
{
winnerPos = i;
}
}
}

void calculate_Percentage_Of_Every_Candidate()
{
int i;
for (i=0; i<N ; i++)
{
}
}

void show_output()
{
cout << setprecision(2) << fixed << showpoint << endl;
int i;
cout << "Candidate" << "  " << "Votes Received" << "   " << "Percentage Votes." << endl;
for ( i = 0; i < N; i++ )
{
cout << names[i] << "\t\t" << votes[i] << "\t\t" << percentageVotes[i] << endl;
cout << endl;
}
cout << "Winner of the election: " << names[winnerPos];
}

int main()
{
get_input();
calculate_winner();
calculate_Percentage_Of_Every_Candidate();
show_output();
return 0;
}

Robinson has eight characters in it.  I see your other names seem to have a maximum character length of seven characters.  I notice you are using tabs in your output.  If you use one less tab for writing Robinson output and all names with eight or more characters, it might line up better.

C++

Volunteer

#### Titus B. Ledbetter, Jr.

##### Expertise

C , MFC, Object Oriented, Artificial Intelligence

##### Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins