Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <conio.h>
- using namespace std;
- struct Student {
- string name;
- string egn;
- double physics;
- double maths;
- double c_science;
- };
- void create_file(fstream &, const string);
- void add_students(fstream &, Student[], size_t &);
- void read_students(fstream &, Student[], size_t);
- double average_grade_at_physics(fstream &, Student [], size_t);
- int main() {
- fstream file;
- Student students[30];
- size_t choice, size = 0;
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "[1] Create empty file\n";
- cout << "[2] Add students to file\n";
- cout << "[3] Edit students information\n";
- cout << "[4] Average grade in physics where maths exam is passed\n\n";
- cout << "[0] Exit\n\n";
- cout << "Your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- create_file(file, "students.dat");
- cout << "File created! Press any key to return to the menu...";
- _getch();
- break;
- case 2:
- system("cls");
- add_students(file, students, size);
- break;
- case 3:
- system("cls");
- read_students(file, students, size);
- cout << "\n\nPress any key to return to the menu...";
- _getch();
- break;
- case 4:
- system("cls");
- cout << "The average grade of all students which have passed the maths exam is: " << average_grade_at_physics(file, students, size);
- cout << "\n\nPress any key to return to the menu...";
- _getch();
- break;
- }
- } while (choice != 0);
- return 0;
- }
- void create_file(fstream &file, const string filename) {
- file.open(filename, ios::binary | ios::out);
- file.close();
- }
- void add_students(fstream &file, Student student[], size_t &size) {
- size_t counter = 0;
- char answer;
- bool Running = true;
- file.open("students.dat", ios::binary | ios::app);
- while (Running) {
- cout << "Enter student's name: ";
- cin >> student[counter].name;
- cout << "Enter student's EGN: ";
- cin >> student[counter].egn;
- cout << "Enter student's grade at physics: ";
- cin >> student[counter].physics;
- cout << "Enter student's grade at maths: ";
- cin >> student[counter].maths;
- cout << "Enter student's grade at computer science: ";
- cin >> student[counter].c_science;
- cout << "Do you want to continue adding students? (y - yes / n - no)";
- cin >> answer;
- if (toupper(answer) == 'N') {
- Running = !Running;
- }
- counter++;
- }
- size = counter;
- file.write((char *)&student, sizeof(student));
- file.close();
- }
- void read_students(fstream &file, Student student[], size_t size) {
- string egn;
- file.open("students.dat", ios::in | ios::ate | ios::binary);
- file.read(reinterpret_cast<char *> (&student), sizeof(student));
- Student new_student;
- cout << "Enter student egn to search: ";
- cin >> egn;
- for (size_t i = 0; i < size; i++) {
- if (strcmp(egn.c_str(), student[i].egn.c_str())) {
- file.seekg( ( sizeof(student) ), ios::cur);
- cout << "Enter name for student with egn " << student[i].egn << " :";
- cin >> new_student.name;
- cout << "Enter physics grade for student with egn " << student[i].egn << " :";
- cin >> new_student.physics;
- cout << "Enter maths grade for student with egn " << student[i].egn << " :";
- cin >> new_student.maths;
- cout << "Enter computer science grade for student with egn " << student[i].egn << " :";
- cin >> new_student.c_science;
- file.write(reinterpret_cast<char *> (&student), sizeof(student));
- }
- }
- file.close();
- }
- double average_grade_at_physics(fstream &file, Student student[], size_t size) {
- size_t counter = 0;
- double average_grade = 0.0;
- file.open("students.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&student), sizeof(student));
- for (size_t i = 0; i < size; i++) {
- if (student[i].maths > 2) {
- average_grade += student[i].physics;
- counter++;
- }
- }
- file.close();
- return (average_grade / counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement