C++ Format Create a program to handle a college class\'s information and grades:
ID: 3860238 • Letter: C
Question
C++ Format
Create a program to handle a college class's information and grades:
Capture the Teacher's name
Capture the Class designation
The program should ask how many students are in the class and do the following for each student:
Read the students name
Read a maximum of 10 grades for the student (from 0 - 100 is acceptable. If outside the range don't use the input, ask for a correction , 999 should stop input of grades if there are less than 10 to enter)
Calculate the average of the student's grades
Compute the student's grade as a letter grade
For the entire class
Compute the class's grade average
Determine how many A's, B's, C's, D's and F's are in the class.
Write the output data to a file called class_statistics.txt
Teacher: Bob Marley
Class: CGS1010
Student Name: Jim Beam Average: 88 Grade: B
Donna Jenner 95 A
Student count: 2
Student average: 91.5
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
Need to use this Code
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printFileToScreen();
int main()
{
//close the input file here
printFileToScreen();
return 0;
}
void printFileToScreen()
{
ifstream inData;
string line = "";
inData.open("class_statistics.txt");
while(inData)
{
getline(inData,line);
cout << line << endl;
}
inData.close();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
//function prototype
char getGrade(double avg);
int main()
{
//declare variables to read from user
char teacherName[40];
char className[40];
char studentName[40];
int numStudents;
int marks=0;
double totalMarks=0;
double avg=0;
bool repeat=true;
int gradeA=0;
int gradeB=0;
int gradeC=0;
int gradeD=0;
int gradeF=0;
string fileName="class_statistics.txt";
ofstream fout;
fout.open(fileName);
cout<<"Enter teacher's name :";
gets(teacherName);
cout<<"Enter class name :";
gets(className);
fout<<"Teacher: "<<teacherName<<endl;
fout<<"Class: "<<className<<endl;
cout<<"Enter number of students ";
cin>>numStudents;
cin.ignore();
for(int index=0;index<numStudents;index++)
{
cout<<"Enter student name : ";
gets(studentName);
fout<<"Student Name:"<<studentName<<endl;
int grade;
int count=0;
//read ten grades
for(grade=0;grade<10 && repeat;grade++)
{
do
{
cout<<"Enter grade "<<(grade+1)<<" marks or 999 to stop: ";
cin>>marks;
if(marks==999)
repeat=false;
}while((marks<0 || marks>100) && repeat);
if(marks!=999)
{
count++;
//add marks to totalMarks
totalMarks+=marks;
}
}
cin.ignore();
//calculate average
avg=totalMarks/count;
//write average to file
fout<<"Average : "<<setprecision(4)<<avg<<endl;
char gradeLetter=getGrade(avg);
//write grade to file
fout<<"Grade: "<<getGrade(avg)<<endl;
//count grades of each letter
if(gradeLetter=='A')
gradeA++;
else if(gradeLetter=='B')
gradeB++;
else if(gradeLetter=='C')
gradeC++;
else if(gradeLetter=='D')
gradeD++;
else if(gradeLetter=='F')
gradeF++;
//reset
totalMarks=0;
avg=0;
repeat=true;
}
//print grades
fout<<"A's"<<gradeA<<endl;
fout<<"B's"<<gradeB<<endl;
fout<<"C's"<<gradeC<<endl;
fout<<"D's"<<gradeD<<endl;
fout<<"F's"<<gradeF<<endl;
//close file
fout.close();
system("pause");
return 0;
}
//Returns character grade
char getGrade(double avg)
{
char grade=' ';
if(avg>=90)
grade='A';
else if(avg>=80 && avg<90)
grade='B';
else if(avg>=70 && avg<80)
grade='C';
else if(avg>=60 && avg<70)
grade='D';
else
grade='F';
return grade;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.