Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that reads students' names followed by their test scores. The program should output each student's name followed by the test scores and

Write a program that reads 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, testScore 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.

Your program must contain at least the following functions:

  1. A function to read the students' data into the array.
  2. A function to assign the relevant grade to each student.
  3. A function to find the highest test score.
  4. A function to print the names of the students having the highest test score.

Your program must output each student's name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls.

This is what I current have:

studentData:

Liam Smith 21 Olivia Johnson 37 Noah Williams 69 Emma Brown 68 Oliver Jones 84 Ava Garcia 99 William Miller 72 Sophia Davis 81 Elijah Rodriguez 58 Isabella Martinez 91 James Hernandez 86 Charlotte Lopez 70 Benjamin Gonzalez 61 Amelia Wilson 53 Lucas Anderson 83 Mia Thomas 91 Mason Taylor 95 Harper Moore 66 Ethan Jackson 88 Evelyn Martin 11

#include #include #include

using namespace std;

struct studentType { string studentFName; string studentLName; int testScore; char grade; };

//function to read the students data into the array void readData(ifstream &studentData, struct studentType arr[20]) { int currentStudent = 0;

while (!studentData.eof()) { studentData >> arr[currentStudent].studentFName; studentData >> arr[currentStudent].studentLName; studentData >> arr[currentStudent].testScore; currentStudent++; } }

// function to assign the relevant grade to each student void assignedGrade(struct studentType arr[20]) { for (int i = 0; i < 20; i++) { int score = arr[i].testScore;

if (score >= 90) { arr[i].grade = 'A'; } else if (score >= 80) { arr[i].grade = 'B'; } else if (score >= 70) { arr[i].grade = 'C'; } else if (score >= 60) { arr[i].grade = 'D'; } else { arr[i].grade = 'F'; } } }

// highest test scores int gethighestScore(struct studentType arr[20]) { int highestScore = 0; for (int i = 0; i < 20; i++) { if (arr[i].testScore > highestScore) { highestScore = arr[i].testScore; } } return highestScore; }

// display highest grade void displayGrade(ofstream & cout, struct studentType arr[20], int highScore) { cout << "Students with the hightest test score:" << endl; for (int i = 0; i < 20; i++) { if (arr[i].testScore == highScore) { cout << arr[i].studentLName + "," + arr[i].studentFName << endl; } } }

//output void printData(ofstream & cout, struct studentType arr[20]) { cout << "Student Name\t" << "Test Score\t" << "Grade ";

for (int i = 0; i < 20; i++) { cout << arr[i].studentLName + ", " + arr[i].studentFName; cout << arr[i].testScore; cout << arr[i].grade << endl; } cout << endl; }

int main() {

ifstream studentData; studentData.open("studentData.txt");

ofstream out_studentData; out_studentData.open("output.txt");

struct studentType arr[20];

readData(studentData, arr); assignedGrade(arr); printData(out_studentData, arr); int highScore = gethighestScore(arr); displayGrade(out_studentData, arr, highScore);

studentData.close(); out_studentData.close();

return 0; }

Error code I get:

Exception thrown at 0x7AC45DAC (msvcp140d.dll) in Program 2.exe: 0xC0000005: Access violation reading location 0x0090001C.

on line 24.

Where am I going wrong here?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

9.4 Explain the roles in career development.

Answered: 1 week ago