The prompt for the exercise is: Write a program that reads students’ names follo
ID: 672412 • Letter: T
Question
The prompt for the exercise is:
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: a. A function to read the students’ data into the array. b. A function to assign the relevant grade to each student. c. A function to find the highest test score. d. 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.
The text file that was provided to me is simple. There are 20 lines of text, and each line contains (in order) the first name, last name, and test score.
i have this so far but it keeps giveing me a error, the errors are
1.)line 56 Severity Code Description Project File Line
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion) lab 7-2 c:userskingo_000desktopcsc 2110lab 7-2lab 7-2lab7-2.cpp 56
2.) line 74 Severity Code Description Project File Line
Error C2447 '{': missing function header (old-style formal list?) lab 7-2 c:userskingo_000desktopcsc 2110lab 7-2lab 7-2lab7-2.cpp 74
this is wht i have so far:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int numOfStudents = 20;
struct studentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
void calculateGrade(studentType sList[], int listSize)
{
int score;
char grade = 'A';
for
(int i = 0; i < listSize; i++)
{
score = sList[i].testScore;
if (score <= 59)
{
grade = 'F';
}
else if (score >= 60 && score <=69)
{
grade = 'D';
}
else if (score >= 70 && score <= 79)
{
grade = 'C';
}
else if (score >= 80 && score <= 89)
{
grade = 'B';
}
else (score >= 90 && score <= 100);
{
grade = 'A';
}
sList[i].grade = grade;
}
}
void getData(studentType sList[], int listSize)
{
ifstream inFile;
inFile.open("studentgrades.txt", ios::in);
if (inFile == NULL)
{
printf(" Unable to open the input file.");
return;
}
int count = 0;
while (count < listSize)
{
inFile >> sList[count].studentLName >> sList[count].studentFName >> sList[count].testScore;
count++;
}
inFile.close();
}
int highestScore(const studentType sList[], int listSize);
{
int highscore = 0;
for (int i = 0, i < listSize; i++)
{
if (highscore < sList[i].testScore)
{
highscore = sList[i].testScore;
}
}
return highscore;
}
void printResult(const studentType sList[], int listSize)
{
ofstream outFile;
outFile.open("studentgrades.txt", ios::out);
string name = "";
outFile << left << setw(30) << "Student Name" << right << setw(10) << "Test Score" << right << setw(7) << "Grade" << endl;
for (int i = 0; i < listSize; i++)
{
name = sList[i].studentFName + ", " + sList[i].studentLName;
outFile << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}
outFile << endl;
int highscore = highestScore(sList, listSize);
outFile << "Highest Test Score: " << highscore << endl;
outFile << "Students having the highest test score: " << endl;
for (int i = 0; i < listSize; i++)
{
if (sList[i].testScore == highscore)
{
outFile << sList[i].studentFName << ", " << sList[i].studentLName << endl;
}
}
outFile.close();
cout << " The Records were written to output file ";
}
int main()
{
cout << " Program works with student details.";
studentType sList[numOfStudents];
calculateGrade(sList, (int)numOfStudents);
printResult(sList, (int)numOfStudents);
cout << " ";
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int numOfStudents = 20;
struct studentType {
string studentFName;
string studentLName;
int testScore;
char grade;
};
void calculateGrade(studentType sList[], int listSize)
{
int score;
char grade = 'A';
for
(int i = 0; i < listSize; i++)
{
score = sList[i].testScore;
if (score <= 59)
{
grade = 'F';
}
else if (score >= 60 && score <=69)
{
grade = 'D';
}
else if (score >= 70 && score <= 79)
{
grade = 'C';
}
else if (score >= 80 && score <= 89)
{
grade = 'B';
}
else (score >= 90 && score <= 100);
{
grade = 'A';
}
sList[i].grade = grade;
}
}
void getData(studentType sList[], int listSize)
{
ifstream inFile;
inFile.open("studentgrades.txt", ios::in);
if (inFile == NULL)
{
printf(" Unable to open the input file.");
return;
}
int count = 0;
while (count < listSize)
{
inFile >> sList[count].studentLName >> sList[count].studentFName >> sList[count].testScore;
count++;
}
inFile.close();
}
int highestScore(const studentType sList[], int listSize)
{
int highscore = 0;
for (int i = 0; i < listSize; i++)
{
if (highscore < sList[i].testScore)
{
highscore = sList[i].testScore;
}
}
return highscore;
}
void printResult(const studentType sList[], int listSize)
{
ofstream outFile;
outFile.open("studentgrades.txt", ios::out);
string name = "";
outFile << left << setw(30) << "Student Name" << right << setw(10) << "Test Score" << right << setw(7) << "Grade" << endl;
for (int i = 0; i < listSize; i++)
{
name = sList[i].studentFName + ", " + sList[i].studentLName;
outFile << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}
outFile << endl;
int highscore = highestScore(sList, listSize);
outFile << "Highest Test Score: " << highscore << endl;
outFile << "Students having the highest test score: " << endl;
for (int i = 0; i < listSize; i++)
{
if (sList[i].testScore == highscore)
{
outFile << sList[i].studentFName << ", " << sList[i].studentLName << endl;
}
}
outFile.close();
cout << " The Records were written to output file ";
}
int main()
{
cout << " Program works with student details.";
studentType sList[numOfStudents];
calculateGrade(sList, (int)numOfStudents);
printResult(sList, (int)numOfStudents);
cout << " ";
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.