Write a program that will read student\'s ID, first name, last name, and 5 score
ID: 3810141 • Letter: W
Question
Write a program that will read student's ID, first name, last name, and 5 scores from an input file "hw2data. txt". The 5 scores are midterm exam score, final exam score, homework score, lab score, and quiz score. The program calculates a student's weighted total based on the formula: Weighted total 25% (midterm score) 25%(final exam score) 30%(homework score) 10%(lab score) 10% (quiz score And then assigns a letter grade to student using the grade scale 90 weighted total 100 A 80 weighted total 90 70 weighted total 80 60 weighted total 70 0 weighted total 60 The program should output each student's ID, full name, 5 scores, weighted total, and letter grade in a neat format. Your program should define a class Student and implement it as required. The class Student should have the following private member variables. Member Variable Description ID An int variable that holds a student's ID first Name A string variable that holds a student's first name. lastName A string variable that holds a student's last name. An int array that holds a student's 5 scores in the order of midterm exam S Core S score, final exam score, homework score, lab score, and quiz score.Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#define MAX 100
using namespace std;
class Student
{
int ID;
string firstName;
string lastName;
//theare are 5 scores
int scores[5];
public:
Student();
Student(int ID, string fname, string lname, int arr_score[]);
void setID(int id);
void setFName(string fname);
void setLName(string lname);
void setScores(int a[]);
int getID();
string getFName();
string getLName();
float getWeightedTotal();
char getGrade(float weighted_total);
void printStudent();
};
Student::Student()
{
ID = 0;
firstName = "";
lastName = "";
//set all score values to 0
for (int i = 0; i < 5; i++)
scores[i] = 0;
}
Student::Student(int ID, string fname, string lname, int arr_score[])
{
ID = ID;
firstName = fname;
lastName = lname;
//set all score values to 0
for (int i = 0; i < 5; i++)
scores[i] = arr_score[i];
}
void Student::setID(int id)
{
ID = id;
}
void Student::setFName(string fname)
{
firstName = fname;
}
void Student::setLName(string lname)
{
lastName = lname;
}
void Student::setScores(int a[])
{
for (int i = 0; i < 5; i++)
scores[i] = a[i];
}
int Student::getID()
{
return ID;
}
string Student::getFName()
{
return firstName;
}
string Student::getLName()
{
return lastName;
}
float Student::getWeightedTotal()
{
float weighted_total ;
weighted_total = 0.25*scores[0] + 0.25 * scores[1] + 0.30*scores[2] + 0.10*scores[3] + 0.10*scores[4];
return weighted_total;
}
char Student::getGrade(float weighted_total)
{
char grade;
if (weighted_total >= 90 && weighted_total <= 100)
{
grade = 'A';
return grade;
}
if (weighted_total >= 80 && weighted_total < 90)
{
grade = 'B';
return grade;
}
if (weighted_total >= 70 && weighted_total< 80)
{
grade = 'C';
return grade;
}
if (weighted_total >= 60 && weighted_total< 70)
{
grade = 'D';
return grade;
}
if (weighted_total >= 0 && weighted_total < 60)
{
grade = 'E';
return grade;
}
}
void Student::printStudent()
{
float total = getWeightedTotal();
char grade = getGrade(total);
cout << setw(5) << getID() << " " << setw(10) << getFName() << " " << setw(10) << getLName() << " " << setw(6) << total << " " << setw(1) << grade << endl;
}
int main()
{
//declare array of objects students
Student s[MAX];
//declare input stream to read from file hw2data.txt
ifstream in;
//open file student.txt
in.open("hw2data.txt");
//check if file open
if (!in)
{
cout << "Can't open the input file hw2data.txt" << endl;
return -1;
}
//decalre variable to read values from file
int s1[5];
int id,count = 0;;
string fname, lname;
//read from file till EOF(end of file)
while (!in.eof())
{
in >> id >> fname >> lname;
for (int i = 0; i < 5; i++)
in >> s1[i];
//set the student member variable with read values
s[count].setID(id);
s[count].setFName(fname);
s[count].setLName(lname);
//set test scores
for (int i = 0; i < 5; i++)
s[count].setScores(s1);
++count;
}
cout << setw(5) << "ID" << " " << setw(10) << "First_name" << " " << setw(10) << "Last_name" << " " << setw(6) << "Weighted_total" << " " << setw(1) << "Grade" << endl;
for (int i = 0; i < count; i++)
{
//calculate weighted total and grade
s[i].printStudent();
}
}
---------------------------------------------------------------------------------------------------
//output
ID First_name Last_name Weighted_total Grade
12002 Johnson R 81.8 B
11201 Aniston s 85.1 B
13290 Cooper w 59.35 E
14500 Gupta b 68.35 D
13290 Blair a 55.5 E
15678 Clark k 60.35 D
15653 Kennedy p 58.3 E
18976 Bronson s 90.85 A
24576 Sunny v 66.9 D
32456 Smith k 67.75 D
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.