Question

C++ program that reads students' names followed by their test scores (5 scores) from the given...

C++ program that reads students' names followed by their test scores (5 scores) from the given input

file, students.txt. The program should output to a file, output.txt, each student's first name, last name,

followed by the test scores and the relevant grade. All data should be separated by a single space.

Student data should be stored in a struct variable of type StudentType, which has four components:

studentFName and studentLName of type string, an array of testScores of type int and grade of type

char.

Suppose that the class has 20 students. Use an array of 20 components of type StudentType.

The program must contain at least the following functions:

1. A function to read the students' data into an array.

2. A function to assign the relevant grade to each student.

Moreover, other than declaring the variables and opening the input and output files, the function main

should only be a collection of function calls. Use constants where needed. You MUST use structs, an

array of the struct, and an array inside the struct for the scores.

Sample input file:

George Washington 90 100 80 72 66

John Adams 80 75 78 92 83

Thomas Jefferson 99 98 97 96 95

James Madison 72 83 80 93 90

James Monroe 100 89 98 87 78

John Adams 60 70 80 90 100

Andrew Jackson 50 55 60 65 70

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================


=================================

// students.txt (Input file)

George Washington 90 100 80 72 66
John Adams 80 75 78 92 83
Thomas Jefferson 99 98 97 96 95
James Madison 72 83 80 93 90
James Monroe 100 89 98 87 78
John Adams 60 70 80 90 100
Andrew Jackson 50 55 60 65 70

=================================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
struct StudentType
{
string firstName;
string lastName;
int scores[5];
char grade;
};
int readFile(StudentType s[]);
void calculateGrade(StudentType s[],int SIZE);
void writeFile(StudentType s[],int SIZE);
int main() {
//Declaring variables
const int SIZE=20;
StudentType s[SIZE];
int cnt=readFile(s);
calculateGrade(s,cnt);
writeFile(s,cnt);

return 0;
}
int readFile(StudentType s[])
{
string fname,lname;
int scr;
ifstream dataIn;
int cnt=0;
dataIn.open("students.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>fname>>lname)
{
s[cnt].firstName=fname;
s[cnt].lastName=lname;
for(int i=0;i<5;i++)
{
dataIn>>s[cnt].scores[i];
}
cnt++;
  
}
dataIn.close();
}
return cnt;
}
void calculateGrade(StudentType s[],int SIZE)
{
int sum=0;
double average=0;
char gradeLetter;
for(int i=0;i<SIZE;i++)
{
sum=0;
for(int j=0;j<5;j++)
{
sum+=s[i].scores[j];
}
average=((double)sum)/5;
  
if (average >= 90 && average<=100)
gradeLetter = 'A';
else if (average >= 80 && average < 90)
gradeLetter = 'B';
else if (average >= 70 && average < 80)
gradeLetter = 'C';
else if (average >= 60 && average < 70)
gradeLetter = 'D';
else if (average < 60)
gradeLetter = 'F';

s[i].grade=gradeLetter;
}
}
void writeFile(StudentType s[],int SIZE)
{
ofstream dataOut;
dataOut.open("studentsOutFile.txt");
for(int i=0;i<SIZE;i++)
{
dataOut<<setw(15)<<left<<s[i].firstName<<setw(15)<<left<<s[i].lastName;
for(int j=0;j<5;j++)
{
dataOut<<setw(5)<<left<<s[i].scores[j];
}
dataOut<<setw(5)<<left<<s[i].grade<<endl;
}
  
dataOut.close();
  
}

=============================

// studentsOutFile (output file):

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++ program that reads students' names followed by their test scores (5 scores) from the given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. 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 and grade of type char. Suppose that the class has 20 students. Use an array of...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100- 124, 125–149, 150-174, and 175–200. Finally, the program outputs the total number of students, each score range...

  • C++ Student Test Scores

    Write a program in C++ that reads students' names followed by their test scores. The program should output each student's name folloewd by the test scores and therelevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Suppose that the class has 20students. Use an array of 20 components of type studentType.Your program must output each student's name in this form:last name followed by a comma, followed...

  • THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA...

    THIS IS FINAL COURSE ASSIGNMENT, PLEASE FOLLOW ALL REQUIREMENTS. Hello, please help write program in JAVA that reads students’ names followed by their test scores from "data.txt" file. The program should output "out.txt" file where each student’s name is followed by the test scores and the relevant grade, 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 an instance of class variable of type...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • C++: Create a grade book program that includes a class of up to 20 students each...

    C++: Create a grade book program that includes a class of up to 20 students each with 5 test grades (4 tests plus a Final). The sample gradebook input file (CSCI1306.txt) is attached. The students’ grades should be kept in an array. Once all students and their test scores are read in, calculate each student’s average (4 tests plus Final counts double) and letter grade for the class. The output of this program is a tabular grade report that is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT