223k views
0 votes
C++ PROGRAMMING: Student grades pls help...

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 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.

Your program should accept no input and save the output to Ch9_Ex2Out.txt.

C++ PROGRAMMING: Student grades pls help... Write a program that reads students’ names-example-1
C++ PROGRAMMING: Student grades pls help... Write a program that reads students’ names-example-1
C++ PROGRAMMING: Student grades pls help... Write a program that reads students’ names-example-2

1 Answer

6 votes

Answer:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

// Declare struct

struct studentType {

string studentFName;

string studentLName;

int testScore;

char grade;

};

// Function prototypes

void readData(studentType[], int);

void assignGrade(studentType[], int);

int highestScore(studentType[], int);

void printNames(studentType[], int, int);

int main() {

// Declare variables

const int NUM_STUDENTS = 20;

studentType student[NUM_STUDENTS];

// Open files

ifstream inFile;

ofstream outFile;

inFile.open("Ch9_Ex2In.txt");

outFile.open("Ch9_Ex2Out.txt");

// Read data

readData(student, NUM_STUDENTS);

// Assign grades

assignGrade(student, NUM_STUDENTS);

// Find and print highest score

int highest = highestScore(student, NUM_STUDENTS);

outFile << "The highest score is " << highest << endl;

outFile << "The students with the highest score are: " << endl;

printNames(student, NUM_STUDENTS, highest);

// Close files

inFile.close();

outFile.close();

return 0;

}

// Function definitions

void readData(studentType student[], int numStudents) {

for (int i = 0; i < numStudents; i++) {

inFile >> student[i].studentLName >> student[i].studentFName >> student[i].testScore;

}

}

void assignGrade(studentType student[], int numStudents) {

for (int i = 0; i < numStudents; i++) {

if (student[i].testScore >= 90)

student[i].grade = 'A';

else if (student[i].testScore >= 80)

student[i].grade = 'B';

else if (student[i].testScore >= 70)

student[i].grade = 'C';

else if (student[i].testScore >= 60)

student[i].grade = 'D';

else

student[i].grade = 'F';

}

}

int highestScore(studentType student[], int numStudents) {

int highest = 0;

for (int i = 0; i < numStudents; i++) {

if (student[i].testScore > highest)

highest = student[i].testScore;

}

return highest;

}

void printNames(studentType student[], int numStudents, int highest) {

for (int i = 0; i < numStudents; i++) {

if (student[i].testScore == highest)

outFile << left << student[i].studentLName << ", " << student[i].studentFName << endl;

}

}

Step-by-step explanation:

User Leonardo Lanchas
by
8.9k points