Advertisement
Guest User

Dancho

a guest
Dec 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. struct Student {
  10.     string name;
  11.     string egn;
  12.  
  13.     double physics;
  14.     double maths;
  15.     double c_science;
  16. };
  17.  
  18. void create_file(fstream &, const string);
  19. void add_students(fstream &, Student[], size_t &);
  20. void read_students(fstream &, Student[], size_t);
  21. double average_grade_at_physics(fstream &, Student [], size_t);
  22.  
  23. int main() {
  24.     fstream file;
  25.     Student students[30];
  26.    
  27.     size_t choice, size = 0;
  28.  
  29.     do {
  30.         system("cls");
  31.         cout << "Menu: \n";
  32.         cout << "[1] Create empty file\n";
  33.         cout << "[2] Add students to file\n";
  34.         cout << "[3] Edit students information\n";
  35.         cout << "[4] Average grade in physics where maths exam is passed\n\n";
  36.         cout << "[0] Exit\n\n";
  37.         cout << "Your choice: ";
  38.         cin >> choice;
  39.  
  40.         switch (choice) {
  41.             case 1:
  42.                 system("cls");
  43.  
  44.                 create_file(file, "students.dat");
  45.  
  46.                 cout << "File created! Press any key to return to the menu...";
  47.                 _getch();
  48.                 break;
  49.  
  50.             case 2:
  51.                 system("cls");
  52.  
  53.                 add_students(file, students, size);
  54.                 break;
  55.  
  56.             case 3:
  57.                 system("cls");
  58.  
  59.                 read_students(file, students, size);
  60.  
  61.                 cout << "\n\nPress any key to return to the menu...";
  62.                 _getch();
  63.                 break;
  64.             case 4:
  65.                 system("cls");
  66.  
  67.                 cout << "The average grade of all students which have passed the maths exam is: " << average_grade_at_physics(file, students, size);
  68.  
  69.                 cout << "\n\nPress any key to return to the menu...";
  70.                 _getch();
  71.                 break;
  72.         }
  73.     } while (choice != 0);
  74.  
  75.  
  76.     return 0;
  77. }
  78.  
  79. void create_file(fstream &file, const string filename) {
  80.     file.open(filename, ios::binary | ios::out);
  81.     file.close();
  82. }
  83.  
  84. void add_students(fstream &file, Student student[], size_t &size) {
  85.     size_t counter = 0;
  86.     char answer;
  87.     bool Running = true;
  88.  
  89.     file.open("students.dat", ios::binary | ios::app);
  90.  
  91.     while (Running) {
  92.         cout << "Enter student's name: ";
  93.         cin >> student[counter].name;
  94.  
  95.         cout << "Enter student's EGN: ";
  96.         cin >> student[counter].egn;
  97.  
  98.         cout << "Enter student's grade at physics: ";
  99.         cin >> student[counter].physics;
  100.  
  101.         cout << "Enter student's grade at maths: ";
  102.         cin >> student[counter].maths;
  103.         cout << "Enter student's grade at computer science: ";
  104.         cin >> student[counter].c_science;
  105.  
  106.         cout << "Do you want to continue adding students? (y - yes / n - no)";
  107.         cin >> answer;
  108.  
  109.         if (toupper(answer) == 'N') {
  110.             Running = !Running;
  111.         }
  112.         counter++;
  113.  
  114.     }
  115.  
  116.     size = counter;
  117.  
  118.     file.write((char *)&student, sizeof(student));
  119.     file.close();
  120. }
  121.  
  122. void read_students(fstream &file, Student student[], size_t size) {
  123.  
  124.     string egn;
  125.  
  126.     file.open("students.dat", ios::in | ios::ate | ios::binary);
  127.     file.read(reinterpret_cast<char *> (&student), sizeof(student));
  128.  
  129.     Student new_student;
  130.  
  131.     cout << "Enter student egn to search: ";
  132.     cin >> egn;
  133.  
  134.     for (size_t i = 0; i < size; i++) {
  135.         if (strcmp(egn.c_str(), student[i].egn.c_str())) {
  136.             file.seekg( ( sizeof(student) ), ios::cur);
  137.  
  138.             cout << "Enter name for student with egn " << student[i].egn << " :";
  139.             cin >> new_student.name;
  140.  
  141.             cout << "Enter physics grade for student with egn " << student[i].egn << " :";
  142.             cin >> new_student.physics;
  143.  
  144.             cout << "Enter maths grade for student with egn " << student[i].egn << " :";
  145.             cin >> new_student.maths;
  146.  
  147.             cout << "Enter computer science grade for student with egn " << student[i].egn << " :";
  148.             cin >> new_student.c_science;
  149.  
  150.             file.write(reinterpret_cast<char *> (&student), sizeof(student));
  151.         }
  152.     }
  153.  
  154.  
  155.  
  156.     file.close();
  157. }
  158.  
  159. double average_grade_at_physics(fstream &file, Student student[], size_t size) {
  160.  
  161.     size_t counter = 0;
  162.     double average_grade = 0.0;
  163.  
  164.     file.open("students.dat", ios::binary | ios::in);
  165.     file.read(reinterpret_cast<char *> (&student), sizeof(student));
  166.  
  167.     for (size_t i = 0; i < size; i++) {
  168.         if (student[i].maths > 2) {
  169.             average_grade += student[i].physics;
  170.             counter++;
  171.         }
  172.     }
  173.  
  174.     file.close();
  175.    
  176.     return (average_grade / counter);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement