Using c++, 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 class variable of type studentType, which has four private 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 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.
Using c++, 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.
-
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.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images
It says there is an error in the class solution(the followup one), specifically in the read data function students[i].setGrade(); It says it is empty, but when I add the declaration char grade; to the function and change the statement to students[i].setGrade(grade); it still says there is an error. What does it need to be instead?
Here is my code:
#include <iostream>
#include <string>
using namespace std;
const int numstudents = 20;
class studentType
{
private:
string studentFName;
string studentLName;
int testscore;
char grade;
public:
void setName(string first, string last)
{
studentFName = first;
studentLName = last;
}
void setScore(int score)
{
testscore = score;
}
void setGrade(char grade)
{
if (testscore >= 90)
{
grade = 'A';
}
if (testscore <= 90 && testscore >= 80)
{
grade = 'B';
}
if (testscore <= 80 && testscore >= 70)
{
grade = 'C';
}
if (testscore <= 70 && testscore >= 60)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
string getFirstName()
{
return studentFName;
}
string getLastName()
{
return studentLName;
}
int getScores()
{
return testscore;
}
char getGrade()
{
return grade;
}
};
void readData(studentType students[])
{
string firstName, lastName;
int score;
char grade;
for (int i = 0; i < numstudents; i++)
{
cout << "Enter student #" << i + 1 << "'s first name, last name, and test score: ";
cin >> firstName >> lastName >> score;
students[i].setName(firstName, lastName);
students[i].setScore(score);
students[i].setGrade(grade);
}
}
void printData(studentType students[])
{
for (int i = 0; i < numstudents; i++)
{
cout << left << students[i].getLastName() << ", " << students[i].getFirstName() << ": ";
cout << students[i].getScores() << " (" << students[i].getGrade() << ")" << endl;
}
}
void findHighestScore(studentType students[], int& highestScore, string& highestName)
{
highestScore = -1;
for (int i = 0; i < numstudents; i++)
{
if (students[i].getScores() > highestScore)
{
highestScore = students[i].getScores();
highestName = students[i].getLastName() + ", " + students[i].getFirstName();
}
}
}
void printHighestScore(int highestScore, string highestName)
{
cout << "The highest score is " << highestScore << endl;
cout << "The following student(s) received the highest score : " << highestName << endl;
}
int main()
{
studentType students[numstudents];
readData(students);
printData(students);
int highestScore;
string highestName;
findHighestScore(students, highestScore, highestName);
printHighestScore(highestScore, highestName);
system("pause");
return 0;
}
This is supposed to use class type not a struct type?