Question
Write a program that reads students names followed by their test scores. The program should output each students name followed by the test scores and
Write a program that 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 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: 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 the variables and opening the input and output files, the function main should only be a collection of function calls. Your program should accept no input and save the output to Ch9_Ex2Out.txt.
This is what I have but cannot seem to make it work.
this is the data file
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
This is what i have.
#include
using namespace std;
struct studentType { string studentFName; string studentLName; int testScore; char grade; };
void getData(ifstream& inFile, studentType sList[], int listSize); void calculateGrade(studentType sList[], int listSize); int highestScore(const studentType sList[], int listSize); void printResult(const studentType sList[], int listSize);
int main() { ifstream inFile; inFile.open("Ch9_Ex2Data.txt"); if(!inFile); { cout << "Cannot open the input file. Program terminates!" << endl; return 1; } studentType sList[20]; getData(inFile, sList[20]); calculateGrade(sList[20]); printResult(sList[20]); inFile.close(); return 0; }
void getData(ifstream& inFile, studentType sList[], int listSize) { int n=0; while(n < listSize) { inFile >> sList[n].studentLName >> sList[n].studentFName >> sList.testScore; n++; } } void calculateGrade(studentType sList[], int listSize) { int i; for(i = 0; i < listSize; i++) if (sList[i].testScore < 60) sList[i].grade = 'F'; else if (sList[i].testScore < 70) sList[i].grade = 'D'; else if (sList[i].testScore < 80) sList[i].grade = 'C'; else if (sList[i].testScore < 90) sList[i].grade = 'B'; else sList[i].grade = 'A';
}
int highestScore(const studentType sList[], int listSize) { int high = 0, i; for(i = 0; i < listSize; i++) if(high < sList[i].testScore) high = sList[i].testScore; return high; }
void printResult(const studentType sList[], int listSize) { cout << left << setw(30) << "Student Name" << right << setw(10) << "Test score" << right << setw(7) << "Grade" << endl; string name; int high, i; for (i = 0; i < listSize; i++) { name = listSize[i].studentLName + ", " + sList[i].studentFName; cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl; } cout << endl; high = highestScore(sList, listSize); cout << "Highest test score: " << high << endl; cout << "Student with the highest score: " << endl; for (int i = 0; i < listSize; i++) if(sList[i].testScore == high) cout << sList[i].studentLName << ", " << sList[i].studentFName << endl; }
These are the errors.
main.cpp: In function int main(): main.cpp:33:26: error: cannot convert studentType to studentType* for argument 2 to void getData(std::ifstream&, studentType*, int) getData(inFile, sList[20]); ^ main.cpp:34:25: error: cannot convert studentType to studentType* for argument 1 to void calculateGrade(studentType*, int) calculateGrade(sList[20]); ^ main.cpp:35:22: error: cannot convert studentType to const studentType* for argument 1 to void printResult(const studentType*, int) printResult(sList[20]); ^ main.cpp: In function void getData(std::ifstream&, studentType*, int): main.cpp:47:75: error: request for member testScore in sList, which is of pointer type studentType* (maybe you meant to use -> ?) .studentLName >> sList[n].studentFName >> sList.testScore; ^ main.cpp: In function void printResult(const studentType*, int): main.cpp:85:26: error: invalid types int[int] for array subscript name = listSize[i].studentLName + ", " + sList[i]. ^ bash: line 2: ./a.out: No such file or directory
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started