Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <iomanip>
- #include <sstream>
- #include <stdlib.h>
- using namespace std;
- struct Students
- {
- string Surname;
- string Name;
- string Patronymic;
- int Age;
- string Sex;
- int Course;
- double Progress;
- Students(string Su, string Na, string Pa, int Ag, string Se, int Co, double Pr) : Surname(Su), Name(Na), Patronymic(Pa), Age(Ag), Sex(Se), Course(Co), Progress(Pr) {}
- };
- void CreateFile(char FileName[50])
- {
- ofstream F(FileName);
- F.close();
- cout << "txt file to the data base of students ready!" << endl;
- }
- void Save(vector<Students*> &Stud, char FileName[50])
- {
- ofstream F(FileName);
- for (int i = 0; i < Stud.size(); i++)
- {
- F << Stud[i]->Surname << "|";
- F << Stud[i]->Name << "|";
- F << Stud[i]->Patronymic << "|";
- F << Stud[i]->Age << "|";
- F << Stud[i]->Sex << "|";
- F << Stud[i]->Course << "|";
- F << Stud[i]->Progress << endl;
- }
- F.close();
- }
- vector<string> explode(string const &s, char delim)
- {
- vector<string> result;
- istringstream iss(s);
- for (string token; getline(iss, token, delim); )
- {
- result.push_back(move(token));
- }
- return result;
- }
- void Input(vector<Students*> &Stud, char FileName[50])
- {
- ifstream Student(FileName);
- if (!Student){}
- else
- {
- string s;
- vector<string> temp;
- while (getline(Student, s))
- {
- temp = explode(s, '|');
- Stud.push_back(new Students(temp[0], temp[1], temp[2], atoi(temp[3].c_str()), temp[4], atoi(temp[5].c_str()), atof(temp[6].c_str())));
- }
- }
- }
- void Output(vector<Students*> &Stud, char FileName[50])
- {
- int opinion;
- ifstream Student(FileName);
- if (!Student)
- {
- cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
- cin >> opinion;
- if(opinion == 1) CreateFile(FileName);
- }
- else
- {
- int count = 0;
- for (int i = 0; i < Stud.size(); i++)
- {
- count++;
- }
- if (count == 0) cout << "No entries yet!" << endl;
- else
- {
- for (int i = 0; i < Stud.size(); i++)
- {
- cout << i << " ";
- cout << Stud[i]->Surname << " ";
- cout << Stud[i]->Name << " ";
- cout << Stud[i]->Patronymic << " ";
- cout << Stud[i]->Age << " ";
- cout << Stud[i]->Sex << " ";
- cout << Stud[i]->Course << " ";
- cout << Stud[i]->Progress << endl;
- }
- }
- count = 0;
- }
- }
- void AddStudent(vector<Students*> &Stud, char FileName[50])
- {
- int opinion;
- ifstream F(FileName);
- if (!F)
- {
- cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
- cin >> opinion;
- if(opinion == 1)
- {
- CreateFile(FileName);
- Stud.push_back(new Students("", "", "", 0, "", 0, 0));
- cout << "Surname: "; cin >> Stud[Stud.size()-1]->Surname;
- cout << "Name: "; cin >> Stud[Stud.size()-1]->Name;
- cout << "Patronymic: "; cin >> Stud[Stud.size()-1]->Patronymic;
- cout << "Age: "; cin >> Stud[Stud.size()-1]->Age;
- cout << "Sex: "; cin >> Stud[Stud.size()-1]->Sex;
- cout << "Course: "; cin >> Stud[Stud.size()-1]->Course;
- cout << "Progress: "; cin >> Stud[Stud.size()-1]->Progress;
- Save(Stud, FileName);
- }
- }
- else
- {
- Stud.push_back(new Students("", "", "", 0, "", 0, 0));
- cout << "Surname: "; cin >> Stud[Stud.size()-1]->Surname;
- cout << "Name: "; cin >> Stud[Stud.size()-1]->Name;
- cout << "Patronymic: "; cin >> Stud[Stud.size()-1]->Patronymic;
- cout << "Age: "; cin >> Stud[Stud.size()-1]->Age;
- cout << "Sex: "; cin >> Stud[Stud.size()-1]->Sex;
- cout << "Course: "; cin >> Stud[Stud.size()-1]->Course;
- cout << "Progress: "; cin >> Stud[Stud.size()-1]->Progress;
- Save(Stud, FileName);
- }
- }
- void FindSttudentsNCourse(vector<Students*> &Stud, char FileName[50])
- {
- int opinion;
- ifstream Student(FileName);
- if (!Student)
- {
- cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
- cin >> opinion;
- if(opinion == 1) CreateFile(FileName);
- }
- else
- {
- int count = 0;
- for (int i = 0; i < Stud.size(); i++)
- {
- count++;
- }
- if (count == 0) cout << "No entries yet!" << endl;
- else
- {
- char New[50] = "/Users/CoolGeek/Documents/SixStudent.txt";
- ofstream F(New);
- int temp;
- cout << "Input course: ";
- cin >> temp;
- for (int i = 0; i < Stud.size(); i++)
- {
- if (Stud[i]->Course == temp)
- {
- F << i << " ";
- F << Stud[i]->Surname << " ";
- F << Stud[i]->Name << " ";
- F << Stud[i]->Patronymic << " ";
- F << Stud[i]->Age << " ";
- F << Stud[i]->Sex << " ";
- F << Stud[i]->Course << " ";
- F << Stud[i]->Progress << endl;
- }
- }
- cout << "txt file to the data base of students ready!" << endl;
- F.close();
- }
- count = 0;
- }
- }
- void DeleteStudent(vector<Students*> &Stud, char FileName[50])
- {
- int opinion;
- ifstream F(FileName);
- if(!F)
- {
- cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
- cin >> opinion;
- if(opinion == 1)
- {
- CreateFile(FileName);
- }
- }
- else
- {
- int count = 0;
- for (int i = 0; i < Stud.size(); i++)
- {
- count++;
- }
- if (count == 0) cout << "No entries yet!" << endl;
- else
- {
- int temp;
- cout << "Input ID: ";
- cin >> temp;
- Stud.erase(Stud.begin() + temp);
- Save(Stud, FileName);
- }
- count = 0;
- }
- }
- void EditStudent(vector<Students*> &Stud, char FileName[50])
- {
- int opinion;
- ifstream F(FileName);
- if (!F)
- {
- cout << "File not found!" << endl << "Do you want to create a file for the data base of students?" << endl << "1. Yes" << endl << "2. No " << endl;
- cin >> opinion;
- if(opinion == 1)
- {
- CreateFile(FileName);
- }
- }
- else
- {
- int count = 0;
- for (int i = 0; i < Stud.size(); i++)
- {
- count++;
- }
- if (count == 0) cout << "No entries yet!" << endl;
- else
- {
- int temp;
- cout << "Input ID: ";
- cin >> temp;
- cout << "Surname: "; cin >> Stud[temp]->Surname;
- cout << "Name: "; cin >> Stud[temp]->Name;
- cout << "Patronymic: "; cin >> Stud[temp]->Patronymic;
- cout << "Age: "; cin >> Stud[temp]->Age;
- cout << "Sex: "; cin >> Stud[temp]->Sex;
- cout << "Course: "; cin >> Stud[temp]->Course;
- cout << "Progress: "; cin >> Stud[temp]->Progress;
- }
- count = 0;
- }
- }
- int main()
- {
- vector<Students*> Stud;
- char FileName[50] = "/Users/CoolGeek/Documents/students.txt";
- int menu;
- do
- {
- cout << endl << "1. Create a txt file to store database (required for first start)" << endl
- << "2. View the contents of a databaseĐ°." << endl
- << "3. Add an entry in the database." << endl
- << "4. Delete an entry from the database." << endl
- << "5. Edit entry in the database" << endl
- << "6. Students N rate" << endl
- << "0. Exit" << endl << endl;
- cout << "Select action: "; cin >> menu;
- switch (menu) {
- case 1:
- {
- ifstream F(FileName);
- if (!F) CreateFile(FileName);
- else cout << "The file has already been created and is ready to use!" << endl;
- break;
- }
- case 2:
- {
- Input(Stud, FileName);
- Output(Stud, FileName);
- Stud.clear();
- break;
- }
- case 3:
- {
- Input(Stud, FileName);
- AddStudent(Stud, FileName);
- Stud.clear();
- break;
- }
- case 4:
- {
- Input(Stud, FileName);
- DeleteStudent(Stud, FileName);
- Stud.clear();
- break;
- }
- case 5:
- {
- Input(Stud, FileName);
- EditStudent(Stud, FileName);
- Stud.clear();
- break;
- }
- case 6:
- {
- Input(Stud, FileName);
- FindSttudentsNCourse(Stud, FileName);
- Stud.clear();
- break;
- }
- case 0:
- {
- cout << "Bye!" << endl;
- break;
- }
- default:
- {
- cout << "ERROR!" << endl;
- break;
- }
- }
- }
- while (menu != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement