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

I am writing a c++ progam. I need help with student id and file reading. In this

ID: 3578309 • Letter: I

Question

I am writing a c++ progam. I need help with student id and file reading. In this program, I am trying to read student first name, last name, student ID, student Exams, average and their letter grades.

/////////////////////////////////////////////////

/////////////////////////////////////////

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

const int NUM_Exams = 3;
const int NUM_Students = 4;

struct studata
{
   float exams[NUM_Exams];
   float average;
   string firstName;
   string lastName;
   char letterGrade;
   int stuID;
};
void menu();
void letterGrade(studata Exams[]);
void average(studata Exams[]);
void displayAverage(studata Exams[]);
void readData(studata Exams[], ifstream&inFile, string fileName);
void writeData(studata Exams[], ofstream&outFile, string fileName);
void sortDataHighesttoLowest(studata Exams[]);
void searchwithLastName(studata Exams[], string targetaName);

int main()
{
   int choice;
   ifstream inFile;
   string fileName, firstName, lastName;
   studata Exams[NUM_Students];
   double classAverage;
   int studentCount = 1;
   bool again = false;
   bool hasGrade = false;
   ofstream outFile;
   string targetaName;

   while (!again)
   {
       menu();
       cout << "Please choose an options: ";
       cin >> choice;
       cout << endl;

       switch (choice)
       {
       case 1:

           //this case will ask user to enter the file name
           cout << "What is the name of the text file. ";
           cin >> fileName;
           readData(Exams, inFile, fileName);

           hasGrade = true;
           continue;


       case 2:
           //this case will display the student letter grades and average
           while (!hasGrade)
           {
               cout << "Grades does not exist yet." << endl;
           }

           average(Exams);
           displayAverage(Exams);

           continue;

       case 3:

           //this case will display the class average
           while (!hasGrade)
           {
               cout << "Grades does not exist yet." << endl;
           }
           classAverage = 0;
           for (int i = 0; i < studentCount; i++)
           {
               classAverage += Exams[i].average;
           }
           classAverage /= studentCount;
           cout << setprecision(2) << fixed << endl;
           cout << "The class average is " << classAverage << endl;
           continue;

       case 4:

           //this case will ask user to enter the exact name for output file
           while (!hasGrade)
           {
               cout << "Grades does not exist yet." << endl;
           }
           cout << "What is the exact file name, where you would like to write data. ";
           cin >> fileName;
           writeData(Exams, outFile, fileName);
           continue;

       case 5:

           //this case will sort data from highest to lowest
           while (!hasGrade)
           {
               cout << "Grades does not exist yet." << endl;
           }
           sortDataHighesttoLowest(Exams);
           continue;


       case 6:
           //this case will let user to serach student with their last name.
           cout << "Please enter last student last name: ";
           cin >> targetaName;
           searchwithLastName(Exams, targetaName);

           continue;

       default:
           again = true;
           break;
       }
   }

   system("pause");
   return 0;

}
void menu()
{
   cout << "1. To read data from text file." << endl;
   cout << "2. To display all students data, including letterGrade and average. " << endl;
   cout << "3. To write data on text file within user given name." << endl;
   cout << "4. To Sort data highest to lowest." << endl;
   cout << "5. To serach student with their last name." << endl;
}

void letterGrade(studata Exams[])
{
   //using if else to calculote the class letter grades and using for loop
   for (int i = 0; i < NUM_Students; i++)
   {
       if (Exams[i].average >= 90)
           Exams[i].letterGrade = 'A';

       else if (Exams[i].average >= 80)
           Exams[i].letterGrade = 'B';

       else if (Exams[i].average >= 70)
           Exams[i].letterGrade = 'C';

       else if (Exams[i].average >= 60)
           Exams[i].letterGrade = 'D';

       else Exams[i].letterGrade = 'F';

   }
}
void average(studata Exams[])
{
   //using for loop to calculte the class average
   for (int i = 0; i < NUM_Students; i++)
   {

       cout << showpoint << fixed << setprecision(2);
       Exams[i].average = (Exams[i].exams[0] + Exams[i].exams[1]
           + Exams[i].exams[2]) / 3;

   }
   letterGrade(Exams);
}
//Modified display function
void displayAverage(studata Exams[])
{
   cout << "--------------------------------------------------------------------------------" << endl;
   cout << setw(10) << "FirstName"
       << setw(10) << "LastName"
       <<setw(10) <<"StudID"
       << setw(10) << "Exam 1"
       << setw(10) << "Exam 2"
       << setw(10) << "Exam 3"
       << setw(10) << "Average"
       << setw(13) << "letter Grade" << endl;
   cout << "--------------------------------------------------------------------------------" << endl;

   //using for loop to display the grades
   for (int i = 0; i < NUM_Students; i++)
   {
       cout << left << setw(15);
       cout << Exams[i].firstName << setw(10) << Exams[i].lastName << setw(10) <<Exams[i].stuID <<
           setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
           << setw(10) << Exams[i].exams[2] << setw(10) <<
           Exams[i].average << setw(13) << Exams[i].letterGrade << endl;
   }
}
//Modified readData method
void readData(studata Exams[], ifstream&inFile, string fileName)
{
   inFile.open(fileName);
   string firstLine;
   int i = 0;

   if (inFile.fail())
   {
       cout << "Error occur while opening file." << endl;
       system("pause");
       exit(1);
   }


   //read using the file input stream inFile iteself until end of the file
   //and store in the studata structure
   while (inFile >> Exams[i].firstName >> Exams[i].lastName >>setw(10) >>Exams[i].stuID
        >> Exams[i].exams[0] >> Exams[i].exams[1] >>
       Exams[i].exams[2] >> Exams[i].average >> Exams[i].letterGrade)
   {
       i++;
   }
   inFile.close();
}
void writeData(studata Exams[], ofstream&outFile, string fileName)

{
   outFile.open(fileName);

   //using for loop
   for (int i = 0; i < NUM_Students; i++)
   {
       outFile << left << setw(15);
       outFile << Exams[i].firstName << setw(10) << Exams[i].lastName << setw(10) <<Exams[i].stuID <<
           setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
           << setw(10) << Exams[i].exams[2] << setw(10) <<
           Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
   }
   outFile.close();

}
void sortDataHighesttoLowest(studata Exams[])
{
   studata temp;
   for (int i = 0; i < NUM_Students; i++)
       for (int j = 0; j< NUM_Students; j++)
       {
           if (Exams[i].average > Exams[j].average) //If you want to sort high to low instead of low to high, change the < sign to a > sign
           {
               temp = Exams[i];
               Exams[i] = Exams[j];
               Exams[j] = temp;
           }
       }
}
void searchwithLastName(studata Exams[], string targetaName)
{
   bool firstFound = true;
   for (int i = 0; i < NUM_Students; i++)
   {
       if (Exams[i].lastName.compare(targetaName) == 0)
       {
           if (firstFound)
           {
               cout << "-----------------------------------------------------------------------------------------" << endl;
               cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
                   << setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
                   << "Average" << setw(10) << "letter Grade" << endl;
               cout << "----------------------------------------------------------------------------------------" << endl;
               firstFound = false;
           }
           cout << left << setw(15);
           cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
               setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
               << setw(10) << Exams[i].exams[2] << setw(10) <<
               Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
       }
   }
}

Explanation / Answer

Hello,

Please find my code

Change Points

-------------------------------------

1)Arrange Structure as per file data , this may not be required but good

     For example
student one    12345 70 70 80   ==> FirstName,LastName,Student Id,Exam1 Marks, Exam2

                                                                         Marks,Exam3 Marks

2)I changed reading the File

     

while(!(inFile.eof()) && i < NUM_Students) {

        inFile>>Exams[i].firstName;
        inFile>>Exams[i].lastName;
        inFile>>Exams[i].stuID;
        inFile>>Exams[i].exams[0];
        inFile>>Exams[i].exams[1];
        inFile>>Exams[i].exams[2];
        i++;
    }

3) Now successfully reading file and Student Id

Please check the code and let me know if you face any other problem

Code :-

---------------------

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;

const int NUM_Exams = 3;
const int NUM_Students = 4;
/**
* @brief The studata struct
* Align Information as per data saved in File
* For example
* student one    12345 70 70 80   ==> FirstName,LastName,Student Id,Exam1 Marks, Exam2 Marks,Exam3 Marks
*
*/
struct studata
{
    //Data in the File
    string firstName;
    string lastName;
    int stuID;
    float exams[NUM_Exams];
    //Below need to calculate
    float average;
    char letterGrade;

};
void menu();
void letterGrade(studata Exams[]);
void average(studata Exams[]);
void displayAverage(studata Exams[]);
void readData(studata Exams[], ifstream&inFile, string fileName);
void writeData(studata Exams[], ofstream&outFile, string fileName);
void sortDataHighesttoLowest(studata Exams[]);
void searchwithLastName(studata Exams[], string targetaName);

int main()
{
    int choice;
    ifstream inFile;
    string fileName, firstName, lastName;
    studata Exams[NUM_Students];
    double classAverage;
    int studentCount = 1;
    bool again = false;
    bool hasGrade = false;
    ofstream outFile;
    string targetaName;

    while (!again)
    {
        menu();
        cout << "Please choose an options: ";
        cin >> choice;
        cout << endl;

        switch (choice)
        {
        case 1:

            //this case will ask user to enter the file name
            cout << "What is the name of the text file. ";
            cin >> fileName;
            readData(Exams, inFile, fileName);

            hasGrade = true;
            continue;


        case 2:
            //this case will display the student letter grades and average
            while (!hasGrade)
            {
                cout << "Grades does not exist yet." << endl;
            }

            average(Exams);
            displayAverage(Exams);

            continue;

        case 3:

            //this case will display the class average
            while (!hasGrade)
            {
                cout << "Grades does not exist yet." << endl;
            }
            classAverage = 0;
            for (int i = 0; i < studentCount; i++)
            {
                classAverage += Exams[i].average;
            }
            classAverage /= studentCount;
            cout << setprecision(2) << fixed << endl;
            cout << "The class average is " << classAverage << endl;
            continue;

        case 4:

            //this case will ask user to enter the exact name for output file
            while (!hasGrade)
            {
                cout << "Grades does not exist yet." << endl;
            }
            cout << "What is the exact file name, where you would like to write data. ";
            cin >> fileName;
            writeData(Exams, outFile, fileName);
            continue;

        case 5:

            //this case will sort data from highest to lowest
            while (!hasGrade)
            {
                cout << "Grades does not exist yet." << endl;
            }
            sortDataHighesttoLowest(Exams);
            continue;


        case 6:
            //this case will let user to serach student with their last name.
            cout << "Please enter last student last name: ";
            cin >> targetaName;
            searchwithLastName(Exams, targetaName);

            continue;

        default:
            again = true;
            break;
        }
    }

    system("pause");
    return 0;

}
void menu()
{
    cout << "1. To read data from text file." << endl;
    cout << "2. To display all students data, including letterGrade and average. " << endl;
    cout << "3. To write data on text file within user given name." << endl;
    cout << "4. To Sort data highest to lowest." << endl;
    cout << "5. To serach student with their last name." << endl;
}

void letterGrade(studata Exams[])
{
    //using if else to calculote the class letter grades and using for loop
    for (int i = 0; i < NUM_Students; i++)
    {
        if (Exams[i].average >= 90)
            Exams[i].letterGrade = 'A';

        else if (Exams[i].average >= 80)
            Exams[i].letterGrade = 'B';

        else if (Exams[i].average >= 70)
            Exams[i].letterGrade = 'C';

        else if (Exams[i].average >= 60)
            Exams[i].letterGrade = 'D';

        else Exams[i].letterGrade = 'F';

    }
}
void average(studata Exams[])
{
    //using for loop to calculte the class average
    for (int i = 0; i < NUM_Students; i++)
    {

        cout << showpoint << fixed << setprecision(2);
        Exams[i].average = (Exams[i].exams[0] + Exams[i].exams[1]
            + Exams[i].exams[2]) / 3;

    }
    letterGrade(Exams);
}
//Modified display function
void displayAverage(studata Exams[])
{
    cout << "--------------------------------------------------------------------------------" << endl;
    cout << setw(10) << "FirstName"
        << setw(10) << "LastName"
        <<setw(10) <<"StudID"
        << setw(10) << "Exam 1"
        << setw(10) << "Exam 2"
        << setw(10) << "Exam 3"
        << setw(10) << "Average"
        << setw(13) << "letter Grade" << endl;
    cout << "--------------------------------------------------------------------------------" << endl;

    //using for loop to display the grades
    for (int i = 0; i < NUM_Students; i++)
    {
        cout << left << setw(15);
        cout << Exams[i].firstName << setw(10) << Exams[i].lastName << setw(10) <<Exams[i].stuID <<
            setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
            << setw(10) << Exams[i].exams[2] << setw(10) <<
            Exams[i].average << setw(13) << Exams[i].letterGrade << endl;
    }
}
//Modified readData method
void readData(studata Exams[], ifstream&inFile, string fileName)
{
    inFile.open(fileName);
    string firstLine;
    int i = 0;

    if (inFile.fail())
    {
        cout << "Error occur while opening file." << endl;
        system("pause");
        exit(1);
    }


    //read using the file input stream inFile iteself until end of the file
    //and store in the studata structure
    while(!(inFile.eof()) && i < NUM_Students) {

        inFile>>Exams[i].firstName;
        inFile>>Exams[i].lastName;
        inFile>>Exams[i].stuID;
        inFile>>Exams[i].exams[0];
        inFile>>Exams[i].exams[1];
        inFile>>Exams[i].exams[2];
        i++;
    }

    inFile.close();
}
void writeData(studata Exams[], ofstream&outFile, string fileName)

{
    outFile.open(fileName);

    //using for loop
    for (int i = 0; i < NUM_Students; i++)
    {
        outFile << left << setw(15);
        outFile << Exams[i].firstName << setw(10) << Exams[i].lastName << setw(10) <<Exams[i].stuID <<
            setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
            << setw(10) << Exams[i].exams[2] << setw(10) <<
            Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
    }
    outFile.close();

}
void sortDataHighesttoLowest(studata Exams[])
{
    studata temp;
    for (int i = 0; i < NUM_Students; i++)
        for (int j = 0; j< NUM_Students; j++)
        {
            if (Exams[i].average > Exams[j].average) //If you want to sort high to low instead of low to high, change the < sign to a > sign
            {
                temp = Exams[i];
                Exams[i] = Exams[j];
                Exams[j] = temp;
            }
        }
}
void searchwithLastName(studata Exams[], string targetaName)
{
    bool firstFound = true;
    for (int i = 0; i < NUM_Students; i++)
    {
        if (Exams[i].lastName.compare(targetaName) == 0)
        {
            if (firstFound)
            {
                cout << "-----------------------------------------------------------------------------------------" << endl;
                cout << "FirstName" << setw(10) << "LastName" << setw(10) << "Exam 1"
                    << setw(10) << "Exam 2" << setw(10) << "Exam 3" << setw(10)
                    << "Average" << setw(10) << "letter Grade" << endl;
                cout << "----------------------------------------------------------------------------------------" << endl;
                firstFound = false;
            }
            cout << left << setw(15);
            cout << Exams[i].firstName << setw(10) << Exams[i].lastName <<
                setw(10) << Exams[i].exams[0] << setw(10) << Exams[i].exams[1]
                << setw(10) << Exams[i].exams[2] << setw(10) <<
                Exams[i].average << setw(10) << Exams[i].letterGrade << endl;
        }
    }
}

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