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

Chapters 1-6 C++ Assignment. Scenario The faculty is happy with the program for

ID: 3700612 • Letter: C

Question

Chapters 1-6 C++ Assignment.

Scenario The faculty is happy with the program for entering grade averages, but they would like to have the program be more user friendly. The instructors want the program to be able to recover from errors if they enter a category twice or if they enter an invalid category, rather than making them begin at the beginning each time they make an error. They have asked you to make the following modifications to the original project Input 1. The instructors would like the computer program to let them re-enter the proper grade category if they entered a used or invalid option without having to restart the program. Code the program using C++ and add code to be able to input the following: Input: o Student First Name: (use your first name) o Student Last Name (use your last name) Course ID: (entered by user) Implement a function to display a menu to request the type of grade category (lab, project, test or final exam) o As the category is selected, verify that the category: o has not already been entered is valid invalid choice Hint: a loop is great for this! Print a message if the category has already been entered or if it is an Ask the user to reenter another valid category O If the category is lab, then the instructor can enter the Lab average amount If the category is project, then the instructor can enter the Project average amount o O If the category is test, then the instructor can enter the Test average amount If the category is final exam, then the instructor can enter the Final Exam score O NOTE: the grades that are entered are averages, with the exception of the final exam score, which is only one grade score

Explanation / Answer

1.

SourceCode:

#include<iostream>

#include<string>

#include<fstream>

#include<sstream>

using namespace std;

// function to print menu to the user

void printMenu()

{

cout << "Select from the menu." << endl;

cout << "1. Lab" << endl;

cout << "2. Project" << endl;

cout << "3. Test" << endl;

cout << "4. Final Exam" << endl;

cout << "Enter your choice: ";

}

// function to calculate and return the final grade

double getFinalGrade(double l, double p, double t, double f)

{

double score = ( (30 * l) + (30 * p) + (30 * t) + (10 * f) ) / 100;

return score;

}

// function to calculate and return the final letter grade

string getLetterGrade(double score)

{

if(score >= 90 && score <= 100)

return "A+";

else if(score < 90 && score >= 85)

return "A";

else if(score < 85 && score >= 70)

return "B+";

else if(score < 75 && score >= 60)

return "B";

else if(score < 65 && score >= 60)

return "C";

else if(score < 60)

return "F";

}

// function to write the contents to file

void write2file(string content)

{

ofstream outf;

string filename = "GradeInfo.txt";

outf.open(filename.c_str());

if(!outf)

{

cout << "Error! Unable to create file '" << filename << "'" << endl;

exit(-1);

}

outf << content;

outf.close();

}

// function to print greenting and append the contents to be written to file

void printGreeting(string fname, string lname, string courseid, string &content)

{

cout << "*************************" << endl;

cout << "Hello Welcome!" << endl;

cout << fname << " " << lname << endl;

cout << courseid << endl;

cout << "*************************" << endl;

// appending to contents to print to file

content += "************************* ";

content += "Hello Welcome! ";

content += fname + " " + lname + " ";

content += courseid + " ";

content += "************************* ";

}

// main function

int main()

{

// variables to store the status of each score whether they are entered or not

bool labC = false;

bool projectC = false;

bool testC = false;

bool finalexamC = false;

string content = "";

string fname, lname;

string courseid;

cout << "Please enter your first name: ";

getline(cin>>ws, fname);

cout << "Please enter your last name: ";

getline(cin>>ws, lname);

cout << "Please enter the course id: ";

cin >> courseid;

cout << endl;

printGreeting(fname, lname, courseid, content);

double labAvg, projectAvg, testAvg, finalExamScore;

int choice;

cout << endl;

printMenu();

cin >> choice;

// looping

while(true)

{

if(choice == 1)

{

if(!labC)

{

cout << "Enter the lab average: ";

cin >> labAvg;

labC = true;

}

else

{

cout << "Lab average already entered." << endl;

}

}

else if(choice == 2)

{

if(!projectC)

{

cout << "Enter the project average: ";

cin >> projectAvg;

projectC = true;

}

else

{

cout << "Project average already entered." <<endl;

}

}

else if(choice == 3)

{

if(!testC)

{

cout << "Enter the test average: ";

cin >> testAvg;

testC = true;

}

else

{

cout << "Test average already entered." << endl;

}

}

else if(choice == 4)

{

if(!finalexamC)

{

cout << "Enter the final exam score: ";

cin >> finalExamScore;

finalexamC = true;

}

else

{

cout << "Exam score already entered." << endl;

}

}

else

{

cout << "Invalid Choice!" << endl;

}

if(labC && projectC && testC && finalexamC)

{

// variable to change the double to string

ostringstream sc;

double finalScore = getFinalGrade(labAvg, projectAvg, testAvg, finalExamScore);

sc << finalScore;

content += "Final Score: " + sc.str() + " ";

string letterGrade = getLetterGrade(finalScore);

cout << endl;

cout << "Final Numeric score: " << finalScore << endl;

cout << "Letter Grade: " << letterGrade << endl;

content += "Letter Grade: " + letterGrade + " ";

if(letterGrade == "F")

{

cout << "-- Student is Failed --" << endl;

content += "-- Student is Failed -- ";

}

else

{

cout << "-- Student is Passed --" << endl;

content += "-- Student is Passed -- ";

}

write2file(content);

cout << " Data written to file successfully." << endl;

break;

}

cout << endl;

printMenu();

cin >> choice;

}

}