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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <format>
struct Student
{
std::string id;
enum gender { Male = 0, Female = 1 };
gender g { };
std::string fname;
std::string lname;
std::string cname;
double Icount { };
};
int main()
{
std::ifstream myfile("studentdata.txt", std::ios::in);
std::string line;
std::vector<Student> students;
if (!myfile.is_open())
{
std::cerr << "** ERROR OPENING FILE! **\n";
return 1; // terminate, can't continue
}
else
{
// read the number of data lines
std::getline(myfile, line);
int num_lines { std::stoi(line) };
// read the header line and toss
std::getline(myfile, line);
for (int i { }; i < num_lines; ++i)
{
std::getline(myfile, line);
Student student;
std::istringstream data(line);
data >> student.id;
int temp_g;
data >> temp_g;
student.g = Student::gender(temp_g);
data >> student.fname;
data >> student.lname;
data >> student.cname;
data >> student.Icount;
students.push_back(student);
}
myfile.close();
}
std::cout << "There are " << students.size() << " students.\n\n";
const auto format { "{:>5}{:>8}{:>15}{:>15}{:>10}{:>12}\n" };
std::cout << std::format(format, "ID", "Gender", "First Name", "Last Name", "Course", "Attendance");
for (const auto& [id, gender, first, last, course, attendance] : students)
{
std::string temp_gender;
std::cout << std::format(format,
id, (gender != Student::Male ? "Female" : "Male"),
first, last, course, attendance);
}
}
|