Write a program that reads student information from a file. The information incl
ID: 3690872 • Letter: W
Question
Write a program that reads student information from a file. The information includes the students’ name (first and last name) followed by their test scores. The student data will be stored in a struct variable of type StudentType which has three components: studentName of type string, testScore of type int (testScore is between 0 and 100 inclusive). We will assume that a class has at most 20 students.
We will assume that a class has at most 20 students.
Function Prototypes
Listed below are the function prototypes that your program is required to use.
// Function Prototypes
void PrintNameHeader(ostream& out);
bool OpenInputFile(ifstream& infile, string& infilename);
void Pause(); // same as in previous labs
void ReadStudentData(ifstream& infile, StudentType student[],
int& numStudents);
This function reads all the students data into the array. When reading the data, read the student’s first name and last name as separate strings, then read the test score. If the score is in the proper range (0 to 100) add the student to the list. Ignore any students with scores outside the range.Update the number of students in the array, making sure that the number of students does not go beyond the maximum number allowed while reading the data. Use the information that I will give in class regarding the loop that correctly reads and counts the student data.
void AssignGrades(StudentType student[], int numStudents);
This function assigns the grade to every student.
int HighestScore(const StudentType student[], int numStudents);
This function finds the highest test score of all the students and returns its value.
void PrintNamesWithHighestScore(const StudentType student[], int numStudents);
This function prints the names of the students having the highest test score. The function calls the HighestScore() function, then prints out the names of all the students who have the highest score. (Also print the score and the grade.)
void DisplayAllStudents(const StudentType student[], int numStudents);
This function displays the name of each student with his/her score and the student’s grade.
void GetLowHighRangeValues(int& lowRange, int& highRange);
This function asks the user for the low and high values for a range of scores that he/she would like to view. The function checks that the values are in the order 0 =< lowRange =< highRange =< 100. If the values are out of order, an error message is printed and the user is asked to enter the range again. The updated parameters are returned by the function.
void DisplayStudentsInRange(const StudentType student[], int numStudents,
int lowNum, int highNum);
This function displays the students in the low to high range. The range boundaries are passed in as arguments to the function.
void SortStudentsByName(StudentType student[], int numStudents);
This function sorts the student list by name.
void SortStudentsByScore(StudentType student[], int numStudents);
This function sorts the student list by test score from highest to lowest.
The Main Program
Design main() to:
1. Ask the user for the student data input file and open the file.
2. Read the student data from the input file.
3. Assign the grades.
4. Display the list of students. Print the number of students in the class.
5. Display the list of students with the highest score.
6. Set up a do/while loop to allow the user to view students in different ranges.
a. Ask the user for the low and high score range values.
b. Display the students with scores in the selected range.
c. Ask the user if he/she wants to see another range [y/n].
d. Loop if yes, otherwise end the loop.
7. Sort the list by name, then display the list.
8. Sort the list by test score, then display the list.
After displaying the list of students (see steps #4 and #7), you should “pause†the screen so the user can view the list before continuing with the next part of the program. Use the Pause() function.
Design main() so that it is mostly a series of function calls with cout statements. Make the output user friendly by labeling the output with an appropriate message or label.
Explanation / Answer
#include #include #include using namespace std; struct StudentType {string studentName; int testScore;//Between 0 and 100 char grade; }student[20]; void PrintNameHeader(ostream& out); bool OpenInputFile(ifstream& inFile, string& infilename ); //OPEN input file void Pause();// Pause void ReadStudentData(ifstream& infile, StudentType student[], int& ); // Read student infp including first and last name and test score void AssignGrades(StudentType student[], int);//assign grades to each student int HighestScore(const StudentType student[], int );//Get the highest scores void PrintNamesWithHighestScore(const StudentType student[], int);//Print name with highest Scores void DisplayAllStudents(const StudentType student[], int);//Display all students void GetLowHighRangeValues(int& , int&);//for example a student types 50 100 , it will get all students within that range void DisplayStudentsInRange(const StudentType student[], int, int, int);// display students in that range void SortStudentsByName(StudentType student[], int);// sort students by name void SortStudentsByScore(StudentType student[], int);// sort students by test score highest to lowest const int NUM_STUDENTS = 20; int main() { ifstream infile; string inFilename; int count = 0; StudentType student[NUM_STUDENTS]; int numStudents; PrintNameHeader(cout); OpenInputFile(infile,inFilename); ReadStudentData(infile, student, numStudents); AssignGrades(student, numStudents); return 0; } //Function definitions void PrintNameHeader(ostream& out) { //Display name header on screen coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.