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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::cin;
constexpr char delim { '-' };
class Student {
std::string name, college, course, student_no;
public:
friend std::istream& operator>>(std::istream& is, Student& s) {
std::getline(is, s.name, delim);
std::getline(is, s.college, delim);
std::getline(is, s.course, delim);
std::getline(is, s.student_no);
return is;
}
friend std::ostream& operator<< (std::ostream& os, const Student& s) {
return os << s.name << delim << s.college << delim << s.course << delim << s.student_no << '\n';
}
void display() const {
cout << "Name: " << name << "\n";
cout << "College: " << college << "\n";
cout << "Course: " << course << "\n";
cout << "Student ID no.: " << student_no << "\n";
}
void input() {
cout << "Name: ";
std::getline(cin >> std::ws, name);
cout << "College: ";
std::getline(cin, college);
cout << "Course: ";
std::getline(cin, course);
cout << "Student No.: ";
std::getline(cin, student_no);
}
};
class Students {
void adding();
void view() const;
std::string filnam;
public:
Students(const std::string& fn) : filnam(fn) {}
void menu();
};
void Students::menu() {
for (unsigned opt {}; opt != 6; ) {
cout << "\n---STUDENT MANAGEMENT APPLICATION---\n";
cout << "1.Add student.\n";
cout << "2.Delete Record.\n";
cout << "3.Modify Record.\n";
cout << "4.Search Record.\n";
cout << "5.View All Records.\n";
cout << "6.Exit.\n";
cout << "--------------------\n";
cout << "Choice: ";
while (!(cin >> opt) || (opt < 1 || opt > 6)) {
cout << "Invalid option. Please re-enter: ";
cin.clear();
cin.ignore(100, '\n');
}
switch (opt) {
case 1:
for (char x { 'Y' }; x == 'Y' || x == 'y'; ) {
adding();
cout << "Add another student? [Y/N]: ";
while (!(cin >> x) || !(x == 'Y' || x == 'y' || x == 'n' || x == 'N')) {
cin.clear();
cin.ignore(100, '\n');
cout << "Must be Y/N only: ";
}
}
break;
case 5:
view();
break;
case 6:
break;
}
}
}
void Students::adding() {
cout << "----ADD STUDENT----\n";
Student s;
s.input();
if (std::ofstream file { filnam, std::ios::app })
file << s;
else
cout << "Cannot open file\n";
}
void Students::view() const {
if (std::ifstream file { filnam }) {
unsigned total {};
for (Student s; file >> s; ) {
cout << "\nStudent No." << ++total << "\n";
s.display();
}
if (total == 0)
cout << "NO DATA FOUND.";
} else
cout << "Cannot open file";
}
int main() {
Students project { "Student Record.txt" };
project.menu();
}
| |