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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
enum class gender { Male, Female };
class Student {
private:
std::string id {"None"}, fname {"None"}, lname {"None"}, cname {"None"};
int Icount {};
gender g {gender::Female};
public:
Student() {}
Student(const std::string& _id, gender _g, const std::string& _fname, const std::string& _lname, const std::string& _cname, int _Icount) :
fname(_fname), lname(_lname), id(_id), cname(_cname), Icount(_Icount), g(_g) {}
void setfname(const std::string& _fname) { fname = _fname; }
std::string getfname() const { return fname; }
void setlname(const std::string& _lname) { fname = _lname; }
std::string getlname() const { return lname; }
void setid(const std::string& _id) { fname = _id; }
std::string getid() const { return id; }
void setcname(const std::string& _cname) { fname = _cname; }
std::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; }
void print() const {
const std::string gen {g == gender::Female ? "Female" : "Male"};
std::cout << "ID [" << id << "] First Name [" << fname << "] Last Name [" << lname << "] Gender [" << gen << "] Course [" << cname << "] Attendance [" << Icount << "]\n";
}
};
std::vector<Student> readStudents(const std::string& filename) {
std::ifstream f(filename);
if (!f) {
std::cout << "Failed to open file";
exit(1);
}
size_t size {};
std::string temp;
std::vector <Student> students;
f >> size;
std::getline(f, temp);
std::getline(f, temp);
for (size_t i = 0; i < size; ++i) {
int c {}, _Icount {};
std::string _fname, _lname, _id, _cname;
f >> _id >> c >> _fname >> _lname >> _cname >> _Icount;
students.emplace_back(_id, c== 0 ? gender::Male : gender::Female, _fname, _lname, _cname, _Icount);
}
return students;
}
void printStudents(const std::vector<Student>& stds) {
for (const auto& s : stds)
s.print();
}
std::vector<Student> getFemales(const std::vector<Student>& stds) {
std::vector<Student> stf;
for (const auto& s : stds)
if (s.getg() == gender::Female) {
s.print();
stf.push_back(s);
}
return stf;
}
std::vector<Student> getLowAttendance(const std::vector<Student>& stds) {
std::vector<Student> stla;
for (const auto& s : stds)
if (s.getIcount() < 20) {
s.print();
stla.push_back(s);
}
return stla;
}
int main() {
std::vector <Student> stds_vect {readStudents("Text.txt")};
printStudents(stds_vect);
getFemales(stds_vect);
getLowAttendance(stds_vect);
}
|