Write a C++ program that will display the following menu and word accordingly.
A menu which contains functionality that will allow the following choices.
1. Enter the student data file name.
2. Display the average and the letter grade for each student.
3. Sort data from highest to lowest
4. Display the sorted data.
5. Search for a student record by last name.
6. Write the sorted data to the result.txt
7. Exit.
Here's what I have so far can someone help me fix and solve my problems
// Week 13: In Class Exercise - 5 Solution
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
// Declared constants
const int NUM_EXAMS = 3;
const int NUMBER_STUDENTS = 6;
// Structure definition
struct StudInfo
{
string firstName;
string lastName;
int ID;
float exam[NUM_EXAMS];
float avgGrade;
char letterGrade;
};
// Function prototypes
void programDescription();
void letterGrade(StudInfo[]);
void writeStuInfo(ofstream&, StudInfo[]);
void readStuInfo(ifstream&, StudInfo[]);
void calcGrades(StudInfo[]);
void displayStuInfo(const StudInfo[]);
int main()
{
// Program description
programDescription();
// Declare variables
StudInfo stu1[NUMBER_STUDENTS];
ofstream outFile;
ifstream inFile;
char choice;
string filename;
// open file for output
outFile.open("results.txt");
if(outFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
do
{
// Process data as read and display in tabular format
cout << "1. Enter the student data file name. \n" << endl;
cout << "2. Display the average and the letter grade for each student. \n"<< endl;
cout << "3. Sort data from highest to lowest \n";
cout << "4. Display the sorted data. \n"<< endl;
cout << "5. Search for a student record by last name. \n"<< endl;
cout << "6. Write the sorted data to the result.txt \n"<< endl;
cout << "7. Exit. \n"<< endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "\nEnter file name: ";
cin >> filename;
// open file for input
inFile.open(filename.c_str());
if(inFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
readStuInfo(inFile, stu1);
break;
}
//Calculations and handle results
calcGrades(stu1);
displayStuInfo(stu1);
writeStuInfo(outFile, stu1);
// Close data files
inFile.close();
outFile.close();
return 0;
}
void programDescription()
{
cout << "This program reads student grade information from" << endl
<< "a file, displays it in a tabular format, and" << endl
<< "writes the data to another file." << endl;
}
// Modify function to write contents of entire array of structures to file
void writeStuInfo(ofstream& outFile, StudInfo stu1[])
{
for (int i = 0; i < NUMBER_STUDENTS; i++)
outFile << endl << left << setw(15) <<stu1[i].firstName<< setw(10) << stu1[i].lastName
<< stu1[i].ID << setw(20) << stu1[i].avgGrade << stu1[i].letterGrade;
}
// Modify function to read contents of entire array of structures from file
void readStuInfo(ifstream& inFile, StudInfo stu1[])
{
int i = 0;
while(i < NUMBER_STUDENTS && inFile >> stu1[i].firstName >> stu1[i].lastName >> stu1[i].ID)
{
inFile >> stu1[i].exam[0] >> stu1[i].exam[1] >> stu1[i].exam[2];
i++;
}
}
void calcGrades(StudInfo stu1[])
{
for (int i = 0; i < NUMBER_STUDENTS; i++)
stu1[i].avgGrade = (stu1[i].exam[0] + stu1[i].exam[1] + stu1[i].exam[2]) / 3;
letterGrade(stu1);
}
void letterGrade(StudInfo stu[])
{
for( int i = 0; i < NUMBER_STUDENTS; i++)
{
if(stu[i].avgGrade >= 90)
stu[i].letterGrade = 'A';
else if(stu[i].avgGrade >= 80)
stu[i].letterGrade = 'B';
else if(stu[i].avgGrade >= 70)
stu[i].letterGrade = 'c';
else if(stu[i].avgGrade >= 60)
stu[i].letterGrade = 'D';
else
stu[i].letterGrade = 'F';
}
}
void displayStuInfo(const StudInfo stu1[])
{
// Display header line
cout << endl << endl
<< "Last Exam Exam Exam Overall" << endl
<< "Name 1 2 3 Average" << endl
<< "---- ---- ---- ---- -------" << endl;
// Need loop to display contents of array to screen.
for (int i = 0; i < NUMBER_STUDENTS; i++)
{
cout << left << setw(15);
cout << stu1[i].firstName << stu1[i].lastName << stu1[i].ID << setw(10) << stu1[i].exam[0] << setw(10)
<< stu1[i].exam[1] << setw(10) << stu1[i].exam[2];
cout << setw(10) << stu1[i].avgGrade << setw(10) << stu1[i].letterGrade << endl;
}
}