Question: C++ Programming: Can someone help fix this code please? There's an error that i'm getting.. Any and all help is greatly appreciated! Prompt: Reads students
C++ Programming: Can someone help fix this code please? There's an error that i'm getting.. Any and all help is greatly appreciated!
Prompt:
Reads students names followed by their test scores. The program should output each students 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, 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 elements of type studentType.
Your 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.
Your program must output each students 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 variables and opening the input and output files, the function main() should only be a collection of function calls.
Comment your code and use meaningful or mnemonic variable names.
You are REQUIRED to use the data in the Data.txt file.
Data.txt includes:
Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg 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
RESTRICTIONS: No global variables
No labels or go-to statements
No infinite loops, examples
No break statements
Here's the code:
#include
#include
#include
#include
using namespace std;
//Function Prototypes
struct studentType;
int getHighestScore(studentType** students, int numStudents);
void getStudentsWithScore(int score, studentType** students, int numStudents, studentType*** out, int& outSize);
bool parseFile(string fileName, studentType*** students, int &numStudents);
bool writeFile(string fileName, studentType** students, int numStudents, int scoreHighest, studentType** studentsHighest, int numStudentsHighest);
void setGrades(studentType** students, int numStudents);
void expandArray(studentType*** array, int &size, int increment);
//=============================================================================
struct studentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
int main() {
studentType** students = nullptr;
int numStudents = 0;
if (!parseFile("Data.txt", &students, numStudents)) {
return 1;
}
setGrades(students, numStudents);
int scoreHighest = getHighestScore(students, numStudents);
studentType** studentsHighest = nullptr;
int numStudentsHighest = 0;
getStudentsWithScore(scoreHighest, students, numStudents,
&studentsHighest, numStudentsHighest);
if (!writeFile("Out.txt", students, numStudents, scoreHighest,
studentsHighest, numStudentsHighest)) {
return 1;
}
cout << "Process complete. ";
return 0;
}
int getHighestScore(studentType** students, int numStudents) {
int highestScore = 0;
for (int i = 0; i < numStudents; i++) {
int studentScore = students[i]->testScore;
if (studentScore > highestScore) {
highestScore = studentScore;
}
}
return highestScore;
}
void getStudentsWithScore(int score, studentType** students, int numStudents,
studentType*** out, int& outSize) {
for (int i = 0; i < numStudents; i++) {
studentType* student = students[i];
if (student->testScore == score) {
expandArray(out, outSize, 1);
(*out)[outSize - 1] = student;
}
}
}
bool parseFile(string fileName, studentType*** students, int& numStudents) {
string line;
ifstream stream;
stream.open(fileName, ios::in);
if (stream.fail()) {
cout << " Error opening file '" << fileName << "'. ";
return false;
}
else {
while (getline(stream, line)) {
size_t index;
string forename;
string surname;
string score;
// Wanted to read line by a line and create a stringstream of the
// lines, allowing me to use getline with a space
// delimiter instead of having to do all of this below.
index = line.find(' ');
forename = line.substr(0, index);
line.erase(0, index + 1);
index = line.find(' ');
surname = line.substr(0, index);
line.erase(0, index + 1);
index = line.find(' ');
score = line.substr(0, index);
expandArray(students, numStudents, 1);
(*students)[numStudents - 1] = new studentType;
(*students)[numStudents - 1]->studentFName = forename;
(*students)[numStudents - 1]->studentLName = surname;
(*students)[numStudents - 1]->testScore = stoi(score);
}
stream.close();
return true;
}
}
bool writeFile(string fileName, studentType** students, int numStudents,
int scoreHighest, studentType** studentsHighest, int
numStudentsHighest) {
ofstream stream;
stream.open(fileName, ios::out);
if (stream.fail()) {
cout << " Error creating file '" << fileName << "'. ";
return false;
}
else {
stream << left << setw(28) << "Student Name";
stream << right << setw(12) << "Test Score";
stream << setw(8) << "Grade" << ' ';
for (int i = 0; i < numStudents; i++) {
studentType* student = students[i];
string name = student->studentFName + ", " + student->studentLName;
stream << left << setw(28) << name;
stream << right << setw(12) << student->testScore;
stream << setw(8) << student->grade << ' ';
}
stream << " Highest Test Score: " << scoreHighest << ' ';
stream << "Students having the highest test score: ";
for (int i = 0; i < numStudentsHighest; i++) {
studentType* student = studentsHighest[i];
string name = student->studentFName + ", " + student->studentLName;
stream << left << name << ' ';
}
stream.close();
return true;
}
}
void setGrades(studentType** students, int numStudents) {
for (int i = 0; i < numStudents; i++) {
studentType* student = students[i];
int score = student->testScore;
char grade = 0;
if (score >= 90) {
grade = 'A';
}
else if (score >= 80) {
grade = 'B';
}
else if (score >= 70) {
grade = 'C';
}
else if (score >= 60) {
grade = 'D';
}
else {
grade = 'F';
}
student->grade = grade;
}
}
//ERROR HERE!!
// Would use a template, but it doesn't really matter since this function is
// only used for arrays of type studentType.
void expandArray(studentType*** array, int& size, int increment) {
if (size == 0 || *array == nullptr) {
size = increment;
*array = new studentType*[size];
}
else {
studentType* buffer[size];
for (int i = 0; i < size; i++) {
buffer[i] = (*array)[i];
}
delete[] * array;
size += increment;
*array = new studentType*[size];
for (int i = 0; i < size - increment; i++) {
(*array)[i] = buffer[i];
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
