Grading Program (reading from a text file)
Posted by Samath
Last Updated: January 07, 2021
  5307

Write a grading program for a class with the following grading policies:

a. There are two quizzes, each graded on the basis of 10 points.

b. There is one mid-term exam and one final exam, each graded on the basis of 100 points.

c. The final exam counts for 50% of the grade, the midterm counts for 25% and the two quizzes together count for a total of 25%. (Do not forget to normalize the quiz scores. They should be converted to a percent before they are averaged in.)

Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D and any grade below 60 is an F.

 

The program will read in the student’s scores from a text file (input.txt) and output the student’s record, which consists of two quizzes and two exam scores as well as the student’s average numeric score for the entire course and the final letter grade on the screen. Define and use a structure for the student record. Format of file input.txt is:

quiz1 8

quiz2 9

mid 70

final 80

 

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
 
struct record
{
	int quiz1;
    int quiz2;
    int mid;
    int final;
  float quiz_average;
   float final_grade;
  float midyear_grade;
 float total_grade;	
 
};
 
 
int main () 
{
   record rec;
  int counter = 1;
  string line;
  
  ifstream myfile ("input.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      istringstream iss(line);
      if(counter == 1)
      {
   	  do
         {
           string sub;
           iss >> sub;
           if(sub != "quiz1")
          {
            	rec.quiz1 = atoi(sub.c_str());
            	counter ++;
        	    break;
          }
        
         } 
	  while (iss);
      }
      else if(counter == 2)
      {
   	  do
         {
           string sub;
           iss >> sub;
           if(sub != "quiz2")
          {
            	rec.quiz2 = atoi(sub.c_str());
            	counter++;
        	    break;
          }
        
         } 
	  while (iss);
      }
      else if(counter == 3)
      {
   	  do
         {
           string sub;
           iss >> sub;
           if(sub != "mid")
          {
            	rec.mid = atoi(sub.c_str());
            	counter++;
        	    break;
          }
        
         } 
	  while (iss);
      }
      else if(counter == 4)
      {
   	  do
         {
           string sub;
           iss >> sub;
           if(sub != "final")
          {
            	rec.final = atoi(sub.c_str());
            	counter++;
        	    break;
          }
        
         } 
	  while (iss);
      }
      
    }
    myfile.close();
  }
  else 
  {
    cout << "Unable to open file";
  } 
 
   rec.quiz_average = ((((float)rec.quiz1/10) + ((float)rec.quiz2/10))/2) * .25;
 rec.final_grade = ((float)rec.final/100) * .5;
 rec.midyear_grade = ((float)rec.mid/100) *.25;
 rec.total_grade = rec.quiz_average + rec.final_grade + rec.midyear_grade;
 
  cout <<"Quiz1: "<< rec.quiz1<<endl;
 cout <<"Quiz2: "<< rec.quiz2<<endl;
 cout <<"Mid Grade: " << rec.mid<<endl;
 cout <<"Final exam: "<< rec.final<<endl;
 
 rec.total_grade = rec.total_grade*100;
 cout <<"Final grade: "<<rec.total_grade<<"%"<<endl;
 
 
 cout <<"Letter grade: ";
 if(rec.total_grade > 90)
 {
  cout << "A"<<endl;
 }
 else if((rec.total_grade > 80)&&(rec.total_grade < 90))
 {
  cout << "B"<<endl;
 }
else if((rec.total_grade > 70)&&(rec.total_grade < 80))
 {
  cout << "C"<<endl;
 }
else if((rec.total_grade > 60)&&(rec.total_grade < 70))
 {
  cout << "D"<<endl;
 }
else if(rec.total_grade < 60)
 {
  cout << "F"<<endl;
 }
  return 0;
}