Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- const int sizeRating = 5;
- char filePath[100];
- bool firstRun = true;
- int option;
- struct Stud
- {
- char name[20];
- int groupId;
- int rating[sizeRating];
- };
- void menu (Stud* groupMenu, int &sizeGroup);
- void create(Stud* groupCreate, int &sizeGroup);
- void add(Stud* groupWrite, int &sizeGroup);
- void read(Stud* groupRead, int &sizeGroup);
- int main ()
- {
- int sizeGroup = 2;
- Stud* group = new Stud[sizeGroup];
- cout << "1. Create new database" << endl;
- cout << "2. Complete existing" << endl;
- cout << "3. Read existing" << endl;
- cout << endl;
- while (option <= 0 || option > 3)
- {
- cout << "Select an action 1 - 3: ";
- cin >> option;
- }
- menu(group, sizeGroup);
- return 0;
- }
- void menu (Stud* groupMenu, int &sizeGroup)
- {
- if (!firstRun)
- {
- option = 0;
- cout << "1. Create another database" << endl;
- cout << "2. Complete existing" << endl;
- cout << "3. Read existing" << endl;
- cout << "4. Exit" << endl;
- }
- cout << endl;
- while (option <= 0 || option > 4)
- {
- cout << "Select an action 1 - 4: ";
- cin >> option;
- }
- switch (option)
- {
- case 1:
- create(groupMenu, sizeGroup);
- break;
- case 2:
- add(groupMenu, sizeGroup);
- break;
- case 3:
- read(groupMenu, sizeGroup);
- break;
- case 4:
- break;
- }
- }
- void create(Stud* groupCreate, int &sizeGroup)
- {
- cout << endl;
- cout << "Specify a destination and filename:" << endl;
- cin >> filePath;
- cout << "Students to create: ";
- cin >> sizeGroup;
- FILE* fileCreate = fopen(filePath, "w+b");
- cout << endl;
- for (int i = 0; i < sizeGroup; i++)
- {
- cout << "Student " << i + 1 << endl;
- cout << "Name: ";
- cin >> groupCreate[i].name;
- cout << "Group Id: ";
- cin >> groupCreate[i].groupId;
- cout << "Rating: " << endl;
- for (int j = 0; j < sizeRating; j++)
- {
- cout << j + 1 << ". ";
- cin >> groupCreate[i].rating[j];
- }
- cout << endl;
- }
- fwrite(groupCreate, sizeof(Stud), sizeGroup, fileCreate);
- fclose(fileCreate);
- firstRun = false;
- menu(groupCreate, sizeGroup);
- }
- void add(Stud* groupWrite, int &sizeGroup)
- {
- int addStud = 0;
- cout << endl;
- cout << "Specify a destination and filename:" << endl;
- cin >> filePath;
- cout << "Students to add: ";
- cin >> addStud;
- FILE* fileAdd = fopen(filePath, "a+b");
- cout << endl;
- for (int i = 0; i < addStud; i++)
- {
- cout << "Student " << i + 1 << endl;
- cout << "Name: ";
- cin >> groupWrite[i].name;
- cout << "Group Id: ";
- cin >> groupWrite[i].groupId;
- cout << "Rating: " << endl;
- for (int j = 0; j < sizeRating; j++)
- {
- cout << j + 1 << ". ";
- cin >> groupWrite[i].rating[j];
- }
- cout << endl;
- }
- fwrite(groupWrite, sizeof(Stud), addStud, fileAdd);
- fclose(fileAdd);
- firstRun = false;
- menu(groupWrite, sizeGroup);
- }
- void read(Stud* groupRead, int &sizeGroup)
- {
- cout << endl;
- cout << "Specify a destination and filename:" << endl;
- cin >> filePath;
- FILE* fileRead = fopen(filePath, "r+b");
- int i = 0;
- while (fread(&groupRead[i], sizeof(Stud), 1, fileRead))
- {
- cout << endl;
- cout << "Student: " << i + 1 << endl;
- cout << "Name: "<< groupRead[i].name << endl;
- cout << "Group ID: " << groupRead[i].groupId << endl;
- cout << "Rating: ";
- for (int j = 0; j < sizeRating; j++)
- {
- cout << groupRead[i].rating[j] << " ";
- }
- cout << endl;
- i++;
- }
- cout << endl;
- fclose(fileRead);
- firstRun = false;
- menu(groupRead, sizeGroup);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement