1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
enum class gender { Male, Female };
class Student {
private:
string fname {"None"}, lname {"None"}, id {"None"}, cname {"None"};
int Icount {};
gender g {gender::Female};
public:
Student() {}
Student(const string& _fname, const string& _lname, const string& _id, const string& _cname, int _Icount, gender _g) :
fname(_fname), lname(_lname), id(_id), cname(_cname), Icount(_Icount), g(_g) { }
void setfname(const string& _fname) { fname = _fname; }
string getfname() const { return fname; }
void setlname(const string& _lname) { lname = _lname; }
string getlname() const {return lname; }
void setid(const string& _id) { id = _id; }
string getid() const { return id; }
void setcname(const string& _cname) { cname = _cname; }
string getcname() const { return cname; }
void setIcount(int _Icount) { Icount = _Icount; }
int getIcount() const { return Icount; }
void setg(gender _g) { g = _g; }
gender getg() const { return g; }
};
vector<Student> readStudents(const string& filename) {
size_t size {};
string temp;
vector <Student> students;
ifstream f {filename};
if (!f) {
cout << "Failed to open file";
exit(1);
}
f >> size;
getline(f, temp);
getline(f, temp);
for (size_t i {}; i < size; ++i) {
int c {}, _Icount {};
string _fname, _lname, _id, _cname;
f >> _id >> c >> _fname >> _lname >> _cname >> _Icount;
students.emplace_back(_fname, _lname, _id, _cname, _Icount, c == 0 ? gender::Male : gender::Female);
}
return students;
}
void printStud(const Student& s) {
cout << "ID [" << s.getid() << "] First Name [" << s.getfname() << "] Last Name [" << s.getlname()
<< "] Gender [" << (int)s.getg() << "] Course [" << s.getcname()
<< "] Attendance [" << s.getIcount() << "]\n";
}
void printStudents(const vector<Student>& stds) {
for (const auto& s : stds)
printStud(s);
}
vector<Student> getFemales(const vector<Student>& stds) {
vector<Student> fem;
for (const auto& s : stds)
if (s.getg() == gender::Female)
fem.push_back(s);
return fem;
}
vector<Student> getLowAttendance(const vector<Student>& stds) {
vector<Student> low;
for (const auto& s : stds)
if (s.getIcount() < 20)
low.push_back(s);
return low;
}
int main() {
const auto vect {readStudents("students.txt")};
const auto fem {getFemales(vect)};
const auto low {getLowAttendance(vect)};
std::cout << "The students are:\n";
printStudents(vect);
std::cout << "The female students are:\n";
printStudents(fem);
std::cout << "The low attendances are:\n";
printStudents(low);
}
|