Hey man, you have
C++
, don't you?
#include <iostream>
#include <vector>
#include <array>
#include <sstream>
using namespace std;
class Student
{
string name;
int clas;
int age;
array<int, 5> mark;
public:
double average();
char result(double average);
bool fromUser(const string & prompt);
void toUser(const string & header);
};
bool Student::fromUser(const string & prompt)
{
cout << prompt << endl;
cout << "name: ";
cin >> name;
cout << "class: ";
cin >> clas;
cout << "age: ";
cin >> age;
cout << "now enter 5 marks\n";
for ( auto & m : mark)
cin >> m;
return cin.good();
}
double Student::average()
{
double avg = 0.0;
for (const auto & m : mark)
avg += m;
avg /= mark.size();
return avg;
}
char Student::result(double average)
{
return (average >= 40.0 ? 'P': 'F');
}
void Student::toUser(const string & header)
{
cout << header << endl;
cout << "name: " << name << endl;
cout << "class: " << clas << endl;
cout << "age: " << age << endl;
cout << "marks: " << endl;
for (const auto & m : mark)
cout << " " << m << endl;
double avg = average();
cout << "average: " << avg << endl;
cout << "result: " << result(avg) << endl;
}
int main()
{
vector <Student> student;
constexpr int STUDENTS=2;
for (int i=0; i<STUDENTS; ++i)
{
ostringstream oss;
oss << "please enter data for student " << (i+1) ;
Student s;
s.fromUser(oss.str());
student.push_back(s);
}
cout << "******************************" << endl;
for (int i=0; i<student.size(); ++i)
{
ostringstream oss;
oss << endl << "data of student " << (i+1);
student[i].toUser(oss.str());
}
}