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

Can someone tell me how to create a function to read a text file into an array?

ID: 3664862 • Letter: C

Question

Can someone tell me how to create a function to read a text file into an array? The function main must contain function calls and must open input/output file.

-------

The assignment I have is to write a program that reads 20 students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, test Score of type int (testScore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Function main should only be a collection of function calls.

program must contain at least the following functions:

-A function to read the students’ data into the array.

-A function to assign the relevant grade to each student

-A function to find the highest test score.

-A function to print the names of the students having the highest test score

Input text:

Duckey Donald 85 Goof Goofy 89 Brave Balto 93 Snow Smitn 93 Alice wonderful 89 Samina Akthar 85 Simba Green 95 Donald Egger Brown Deer 86 Johny Jackson 95 Greq Gupta 75 Samuel Happy 80 Danny Arora 80 sleepy June 70 Amy Cheng 83 shelly Malik 95 Chelsea Tomek 95 Angela Clodfelter 95 Allison Nields 95 Lance Norman 88 8 90 5

Explanation / Answer

include <iostream>

#include <fstream>

#include <string>

using namespace std;            // Declare access to standard stuff like cin, cout

struct std1Record                // A structure type to define a new student record via a string library

{

    string firstName;           // This is the student's first name

    string lastName;            // This is the student's last name

    double totscore[100];          // The student's test score

};

// Functions

void Welcome();             // This is to display welcome message to user

void Menu();                // This is to display menu of options

// This is the main function

int main()                      // Main function begins here

{

    ofstream testScores;        // File stream object

   

    // Decleration of variables

   

    char option;                // This is for toring characters through an array

    int numberStudents = 0;        // Total Number of students in the class

    string filename;            // name of the file to store the information of a file name entered by the user

    ifstream fin;               // for File input

    ofstream fout;              // forFile output

   

    // Bool

    bool isInService = true;    // Indicates whether the program should still be actively in service

   

    // Loop begins here         // To enable user to quit the program

    while (isInService == true)

    {

      welcome();

void welcome(){

       

        cout << " Welcome! Please select an option below:" << endl;}

       

      /display();

void display(){

       

        cout << "p) First to input test score information" << endl;

        cout << "r) Second to read the data in the test score file" << endl;

        cout << "d) Third to display the number of students and the list of all the scores" << endl;

        cout << "m) To modify one of the scores" << endl;

        cout << "w) To create a test score file" << endl;

        cout << "q) To QUIT this Program" << endl;

}

       

        // Prompting user to select an option

        cout << "Please select an option (k, r, d, m, w, or q): ";

        cin >> option;

       

        std1Record myStdRecords;                        // Student records structure

       

       // If the usr selects option "p"

        if (option == 'p')                                 //uThe User wants to input test score information

        {

           

            // Get the number of students

            cout << " Please enter the number of students in the class:" << endl;

            cin >> numberStudents;

           

            // Get the student's first name

            for (int anyValue = 0; numberStudents > anyValue; anyValue++)

            {

                cout << " Enter the student's first name: " << endl;

                cin.ignore();                               // To skip the remaining ‘ ' character

                getline(cin, myStdRecords.firstName);

               

                // Get the student's last name

                cout << "Enter the student's last name: " << endl;

                cin.ignore();

                getline(cin, myStdRecords.lastName);

            }

           

            // Get the student's score

            for (int anyValue = 0; numberStudents > anyValue; anyValue++)

            {

                cout << " Please enter the test score: " << endl;

                cin.ignore();

                cin >> myStdRecords.totscore[anyValue];

               

                while (myStdRecords.totscore [anyValue] < 0 || myStdRecords.totscore [anyValue] > 100)

                {

                    cout << " This score is invalid. Please try again. " << endl;

                    cin >> myStdRecords.totscore[100];

                }

               

            }

        }

       

        // the user selected r

       

        if (option == 'r')

        {

           

            ofstream testScores;        // File stream object

           

           

            // to create name for the file

           

            cout << " Please enter a filename:" << endl;

            cin >> filename;

            filename = filename + ".txt";

           

            testScores.open(filename);

           

           

            cout << " File has been created. ";

           

            // Here the user will write data to the file

           

            testScores << numStudents << endl;

           

            for (int anyValue = 0; numStudents > anyValue; anyValue++)

            {

                testScores << myStdRecords.totscore << endl;

            }

           

           

            cout << " The information has been saved to the file. " << endl;

           

           

            // we will now the test score data

            // and elose the file

           

            testScores.close();

           

           

        }

       

        // If the option d is selected

       

       

        if (option == 'd')

        {

            cout << endl << "Number of students: " << numberStudents << endl;

           

           

            // Average

           

            int total = 0;

            for (int anyValue = 0; numStudents > anyValue; anyValue++) {

                total += myStdRecords.totscore[anyValue];

            }

           

            int average = total/numStudents;

           

            cout << "Average score: " << average << endl;

           

            // Highest and lowest score;

           

            int lowestScore = 100;

            int highestScore = 0;

           

            for (int anyValue = 0; numberStudents > anyValue; anyValue++) {

                if (myStdRecords.score[anyValue] > highestScore) {

                    highestScore = myStdRecords.totscore[anyValue];

                }

               

                if (myStdRecords.score[anyValue] < lowestScore) {

                    lowestScore = myStdRecords.totscore[anyValue];

                }

            }

           

            cout << "Highest score: " << highestScore << endl;

            cout << "Lowest score: " << lowestScore << endl;

           

            cout << endl;

       }

       

        if (option == 'm')

        {

            int anyStudent;

           

            cout << " Which student do you want to modify?" <<endl;

            cin >> anyStudent;

            cout << " Please enter a new value: " << endl;

            cin >> myStdRecords.totscore[100];

           

           

        }

       

       

        // If the user selects option "w"

        if (option == 'w')

        {

           

            ofstream testScores;        // File stream object

            stdRecord myStdRecords;     // Student records structure

           

           

            // Here we let the user create a name for the file

           

            cout << " Please enter a filename:" << endl;

            cin >> filename;

            filename = filename + ".txt";

           

            testScores.open(filename);

           

           

            cout << " File has been created. ";

           

            // Here the user will write data to the file

           

            testScores << numStudents << endl;

           

            for (int anyValue = 0; numStudents > anyValue; anyValue++)

            {

                testScores << myStdRecords.totscore[100] << endl;

               

            }

           

           

            cout << " The information has been saved to the file. " << endl;

           

           

            // Program saves the test score data

            // Close the file

           

            testScores.close();

           

           

        }

       

       

        // finally To quit the program

       

        if (option == 'q')

        {

            isInService = false;

           

            cout << "Goodbye!" << endl;

            cin >> option;

            cout << "Please enter any charcter and a return to quit the program." << endl;

           

           

           

            char inputCharacter;    // Declare a variable for storing a character input from the keyboard

            cin >> inputCharacter; // Wait to read in a character input from the keyboard

           

           

        }

       

       

    }   // Loop ends here

   

   

   

   

   

}   // To close the Main Function

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