Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program which reads a student\'s name and five test scores. The prog

ID: 673367 • Letter: W

Question

Write a C++ program which reads a student's name and five test scores.

The program should compute the student's average test score and assign a grade as follows:

90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F.

You must use the following functions:

bool readLine(ifstream &inFile, string &name, int &test1, int &test2, int &test3, int &test4, int &test5);

This function reads one line from the input file, assigning the student's name to the name string variable and the student's five test scores to the five integer reference parameters.

The function returns true is it is able to read a line and make the assignments, false otherwise.

float calculateAverage(int test1, int test2, int test3, int test4, int test5);

This function is called with a student's five grades and calculates their average.

studentGrade determineGrade(float average);

This function is called with a student's average grade and returns the corresponding letter grade, using the enumeration:

enum studentGrade {A, B, C, D, F};

char decodeGrade(studentGrade x);

This function is called with a student's grade in the enumeration and returns the associated grade as a single character.

int main();

This function calls the four functions in a loop until readLine indicates there is no more data.

While there is data, it writes a line to the console echoing the data and reporting the student's average and grade.

You must provide your own file of test grades to demonstrate the program works.

file info :

Smith 89 93 91 75 92
Johnson 68 82 96 99 97
Frank 88 95 96 88 97
Kenshi 90 94 95 99 89
Himaru 92 99 89 95 95
Allenet 79 85 90 93 99
Brown 89 99 92 94 99

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


using namespace std;

enum studentGrade {A, B, C, D, F};


bool readLine(ifstream &inFile, string &name, int &test1, int &test2, int &test3, int &test4, int &test5) {

   string line;

   if(getline (inFile,line)) {

       string word;

       istringstream iss(line, istringstream::in);
       int count = 0;
   try {
       while( iss >> word )
       {
           //cout << word << endl;
           if(count == 0)
               name = word;
           if(count == 1) {
               istringstream(word) >> test1;
           }
           if(count == 2) {
               istringstream(word) >> test1;
           }
           if(count == 1) {
               istringstream(word) >> test2;
           }
           if(count == 3) {
               istringstream(word) >> test3;
           }
           if(count == 4) {
               istringstream(word) >> test4;
           }
           if(count == 5) {
               istringstream(word) >> test5;
           }
                  count++;


       }
           //cout << name << " "<<test1 << " "<<test2 << " "<<test3 << " "<<test4 << " "<<test5 <<endl;

       return true;
   } catch (int e)
      {
         return false;
      }
}
   return false;
}


float calculateAverage(int test1, int test2, int test3, int test4, int test5) {
   float avg = (float) (test1+test2+test3+test4+test5 ) / 5;
   return avg;
}

studentGrade determineGrade(float average) {
//90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F.
   if(average >= 90 && average <= 100)
       return A;
   else if(average >= 80 && average < 90)
       return B;
   else if(average >= 70 && average < 80)
           return C;
   else if(average >= 60 && average < 70)
           return D;
   else
       return F;
}

char decodeGrade(studentGrade x) {
   switch (x) {
   case A :
       return 'A';
   case B :
       return 'B';
   case C :
       return 'C';
   case D :
       return 'D';
   case F :
       return 'F';


   }
   return ' ';
}

int main() {
       string line;
          ifstream myfile ("D:/ravi/Cheg/student.txt");


       if (myfile.is_open())
       {
           bool isValid = false;
           string name;
           int test1 = 0, test2 =0 , test3 =0 , test4 =0, test5 =0;
           while ( myfile.good())
           {
               isValid = readLine(myfile,name, test1, test2, test3, test4, test5);
               if(isValid) {
                   float avg = calculateAverage(test1, test2, test3, test4, test5);
                   studentGrade sgrade = determineGrade(avg);
                   char chgrade = decodeGrade(sgrade);
                   cout << name << " "<<test1 << " "<<test2 << " "<<test3 << " "<<test4 << " "<<test5    << " , Average: "<<avg << " , Grade: "<<chgrade << endl;
               }

           }
           myfile.close();
       }

       else
           cout << "Unable to open file";

}

-----------------------------output-----------------------------------

Smith 93 89 91 75 92 , Average: 88 , Grade: B
Johnson 82 68 96 99 97 , Average: 88.4 , Grade: B
Frank 95 88 96 88 97 , Average: 92.8 , Grade: A
Kenshi 94 90 95 99 89 , Average: 93.4 , Grade: A
Himaru 99 92 89 95 95 , Average: 94 , Grade: A
Allenet 85 79 90 93 99 , Average: 89.2 , Grade: B
Brown 99 89 92 94 99 , Average: 94.6 , Grade: A

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote