Ok, this is c++, and it is an implementation of a class student in three files:
ID: 3915212 • Letter: O
Question
Ok, this is c++, and it is an implementation of a class student in three files: main.cpp, student.h, and student.cpp
What I am stuck at:
In each score for each student, instead of printing just the average, I need to print their scores as well, and then the average.
Also, Mary was living in White Plains, but Mary moves to Pleasantville. I would like to show both, that she lived there, and moved.
Thanks!!
MAIN.CPP
#include <iostream>
#include <iomanip>
#include <string>
#include "student.h"
using namespace std;
int main()
{
Student s1(91313, "Joe", "Smith", "Rye");
Student s2(95791, "Mary", "Jones", "White Plains");
Student s3(98181, "Jessica", "Nascimento", "Stamford");
s1.addScore(65);
s1.addScore(87);
s1.addScore(75);
s2.addScore(90);
s2.addScore(80);
s2.addScore(85);
s3.addScore(100);
s3.addScore(100);
s3.addScore(100);
cout << fixed << setprecision(3);
cout << " Student 1: ";
cout << " Student ID: " << s1.getStudentId();
cout << " Name: " << s1.getFirstName() << " " << s1.getLastName();
cout << " Residence: " << s1.getResidenceTown();
cout << " Score Average: " << s1.getScoreAvg();
cout << " Student 2: ";
cout << " Student ID: " << s2.getStudentId();
cout << " Name: " << s2.getFirstName() << " " << s2.getLastName();
cout << " Residence: " << s2.getResidenceTown();
cout << " Score Average: " << s2.getScoreAvg() << " ";
cout << " Student 3: ";
cout << " Student ID: " << s3.getStudentId();
cout << " Name: " << s3.getFirstName() << " " << s3.getLastName();
cout << " Residence: " << s3.getResidenceTown();
cout << " Score Average: " << s3.getScoreAvg() << " ";
return 0;
}
STUDENT.H
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
int sid;
string lastName;
string firstName;
string residenceTown;
int examScores[3];
int scores;
public:
Student(int id, string fn, string ln, string res);
int getStudentId();
string getLastName();
string getFirstName();
string getResidenceTown();
void setFirstName(string fn);
void setLastName(string ln);
void setResidence(string res);
void addScore(int score);
int* getScores();
double getScoreAvg();
};
STUDENT.CPP
#include<iostream>
#include<string>
#include "student.h"
using namespace std;
Student::Student(int id, string fn, string ln, string res)
{
sid = id;
firstName = fn;
lastName = ln;
residenceTown = res;
scores = 0;
}
int Student::getStudentId()
{
return sid;
}
string Student::getFirstName()
{
return firstName;
}
string Student::getLastName()
{
return lastName;
}
string Student::getResidenceTown()
{
return residenceTown;
}
void Student::setFirstName(string fn)
{
firstName = fn;
}
void Student::setLastName(string ln)
{
lastName = ln;
}
void Student::setResidence(string res)
{
residenceTown = res;
}
void Student::addScore(int sc)
{
examScores[scores] = sc;
scores++;
}
int* Student::getScores()
{
return examScores;
}
double Student::getScoreAvg()
{
int i, sum=0;
for(i=0; i<scores; i++)
{
sum += examScores[i];
}
return (sum/(double)scores);
}
Explanation / Answer
I have changed the code according to your requirements. Now it's printing everything you wanted.
MAIN.CPP
#include <iostream>
#include <iomanip>
#include <string>
#include "student.h"
using namespace std;
int main()
{
Student s1(91313, "Joe", "Smith", "Rye");
Student s2(95791, "Mary", "Jones", "White Plains");
Student s3(98181, "Jessica", "Nascimento", "Stamford");
s1.addScore(65);
s1.addScore(87);
s1.addScore(75);
s2.addScore(90);
s2.addScore(80);
s2.addScore(85);
s3.addScore(100);
s3.addScore(100);
s3.addScore(100);
cout << " Student 1: ";
cout << " Student ID: " << s1.getStudentId();
cout << " Name: " << s1.getFirstName() << " " << s1.getLastName();
cout << " Residence: " << s1.getResidenceTown();
cout << " Scores: " ; s1.printScores();
cout << " Score Average: " << s1.getScoreAvg();
cout << " Student 2: ";
cout << " Student ID: " << s2.getStudentId();
cout << " Name: " << s2.getFirstName() << " " << s2.getLastName();
cout << " Residence: " << "Lived in " << s2.getResidenceTown(); s2.setResidence("Pleasantville");
cout << " but moved to " << s2.getResidenceTown();
cout << " Scores: " ; s2.printScores();
cout << " Score Average: " << s2.getScoreAvg() << " ";
cout << " Student 3: ";
cout << " Student ID: " << s3.getStudentId();
cout << " Name: " << s3.getFirstName() << " " << s3.getLastName();
cout << " Residence: " << s3.getResidenceTown();
cout << " Scores: " ; s3.printScores();
cout << " Score Average: " << s3.getScoreAvg() << " ";
return 0;
}
STUDENT.H
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
int sid;
string lastName;
string firstName;
string residenceTown;
int examScores[3];
int scores;
public:
Student(int id, string fn, string ln, string res);
int getStudentId();
string getLastName();
string getFirstName();
string getResidenceTown();
void setFirstName(string fn);
void setLastName(string ln);
void setResidence(string res);
void addScore(int score);
int* getScores();
double getScoreAvg();
void printScores();
};
STUDENT.CPP
#include<iostream>
#include<string>
#include "student.h"
using namespace std;
Student::Student(int id, string fn, string ln, string res)
{
sid = id;
firstName = fn;
lastName = ln;
residenceTown = res;
scores = 0;
}
int Student::getStudentId()
{
return sid;
}
string Student::getFirstName()
{
return firstName;
}
string Student::getLastName()
{
return lastName;
}
string Student::getResidenceTown()
{
return residenceTown;
}
void Student::setFirstName(string fn)
{
firstName = fn;
}
void Student::setLastName(string ln)
{
lastName = ln;
}
void Student::setResidence(string res)
{
residenceTown = res;
}
void Student::addScore(int sc)
{
examScores[scores] = sc;
scores++;
}
int* Student::getScores()
{
return examScores;
}
double Student::getScoreAvg()
{
int i, sum=0;
for(i=0; i<scores; i++)
{
sum += examScores[i];
}
return (sum/(double)scores);
}
void Student::printScores()
{
int i;
for(i=0; i<scores; i++)
{
cout << examScores[i] << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.