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;
    }
}

Want to give us a hint as to what problems you are encountering? Which functions work, which ones don't?

Sorry the read doesnt work and the menu doesnt work I need the menu to rerun after each choice. As far as the read goes, thats not working and therefore its causing the rest of the other functions to not work.

As for the menu, you start a do...while loop, but there's no while clause. You should complete the switch, even if for the moment you only put dummy actions for the other cases. Then have your while statement closing the loop.

The read function looks sort of OK. I would only read one item in the controlling loop, like:

while(i < NUMBER_STUDENTS && inFile >> stu1[i].firstName )

Then read the remainder of the items inside the loop body. I would also sturcture it to return the loop counter ( i ) to the caller, and store it there so you know how many records were actually read in.

Another minor thing, you should close the infile in the switch section where you opened it, after reading the data. Don't wait till the end of the program. For that matter, what stops someone from re-reading the input file?

Another thing. In the menu section, you take in the user choice into a char variable. Your switch labels must be char as well, using the single quotes around the labels, like:

switch (choice)
{
   case '1': //do stuff
             break;
   case '2': //do other stuff
             break;
    //and so on
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.