Вектор бьёт данные
Решал задачу на C++ следующего содержания:
Необходимо создать базу данных - Работник, с определёнными условиями. Решил не париться с перевыделением памяти и реализовать всё через векторы (все записи в базе данных хранятся в векторе).
Запись в вектор происходит успешно, перепроверял всё, выводил данные прямо в функции добавления новой записи, всё было хорошо. Но когда пытался напечатать Данные с помощью функции печати всех данных - получал битые данные. Битыми они также записывались в файл, вследствие чего я не мог с ними нормально работать.
Проблема: не могу понять, где моя ошибка, и в чём она. Я неправильно сохраняю данные в векторе или я неправильно с ними работаю, в следствие чего они отображаются неверно.
КОД:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#define MaxBuffSize 128
using namespace std;
struct Empoloyee
{
char* Name;
char* SecondName;
int NumOfDepartment;
double Salary;
};
void CreateNewNote(vector <Empoloyee>& AllEmployees, int& NumOfNotes);
void LoadDatabase(vector <Empoloyee>& AllEmployees, int& NumOfNotes);
void PrintMenu();
int GetNumOfFunction();
void PrintAllDatas(vector <Empoloyee> AllEmployees, int& NumOfNotes);
void FindEmployee(vector <Empoloyee> AllEmployees, int& NumOfNotes);
void FilterByDepartment(vector <Empoloyee> AllEmployees, int& NumOfNotes);
void SortBySalary(vector <Empoloyee>& AllEmployees, int& NumOfNotes);
void SaveDatabase(vector <Empoloyee>& AllEmployees, int& NumOfNotes);
void PrintTableForNotes();
void PrintNote(Empoloyee OneNote);
int main() {
int NumOfFunction = 0, NumOfNotes = 0;
vector <Empoloyee> Database;
FILE* FilePointer;
if ((FilePointer = fopen("DataBase.txt", "r")) == NULL)
{
cout << "There is no file, initialization from the keyboard!" << endl;
CreateNewNote(Database, NumOfNotes);
}
else
{
LoadDatabase(Database, NumOfNotes);
cout << "Database has loaded:)" << endl;
cin.clear();
}
system("pause");
system("cls");
while (NumOfFunction != 6) {
PrintMenu();
NumOfFunction = GetNumOfFunction();
switch (NumOfFunction)
{
case 1:
CreateNewNote(Database, NumOfNotes);
break;
case 2:
PrintAllDatas(Database, NumOfNotes);
break;
case 3:
FindEmployee(Database, NumOfNotes);
break;
case 4:
FilterByDepartment(Database, NumOfNotes);
break;
case 5:
SortBySalary(Database, NumOfNotes);
break;
default:
break;
}
}
SaveDatabase(Database, NumOfNotes);
return 0;
}
void CreateNewNote(vector <Empoloyee>& AllEmployees, int& NumOfNotes) {
system("cls");
NumOfNotes += 1;
Empoloyee NewNote;
char Buff[MaxBuffSize];
cout << "Enter the last name of the new employee: ";
gets_s(Buff);
NewNote.SecondName = new char[strlen(Buff) + 1];
strcpy(NewNote.SecondName, Buff);
cout << "Enter the name of the new employee: ";
gets_s(Buff);
NewNote.Name = new char[strlen(Buff) + 1];
strcpy(NewNote.Name, Buff);
cout << "Enter the department number of the new employee: ";
cin >> NewNote.NumOfDepartment;
cout << "Enter the salary of the new employee: ";
cin >> NewNote.Salary;
AllEmployees.push_back(NewNote);
}
void LoadDatabase(vector <Empoloyee>& AllEmployees, int& NumOfNotes) {
Empoloyee Note;
FILE* FilePointer;
char Buff[MaxBuffSize];
ifstream fin("Database.txt");
fin >> NumOfNotes;
for (int i = 0; i < NumOfNotes; i++) {
fin >> Buff;
Note.SecondName = new char(strlen(Buff) + 1);
strcpy(Note.SecondName, Buff);
fin >> Buff;
Note.Name = new char(strlen(Buff) + 1);
strcpy(Note.Name, Buff);
fin >> Note.NumOfDepartment;
fin >> Note.Salary;
AllEmployees.push_back(Note);
}
fin.close();
}
void PrintMenu() {
system("cls");
cout << setw(58) << setfill('.') << left << "Add new employee" << "1" << endl;
cout << setw(58) << left << "Print information about all employees" << "2" << endl;
cout << setw(58) << left << "Find an employee by last name" << "3" << endl;
cout << setw(58) << left << "Print information about all employees of the department" << "4" << endl;
cout << setw(58) << left << "Sort employees in ascending order of salary" << "5" << endl;
cout << setw(58) << left << "Exit the program" << "6" << setfill(' ') << endl;
for (int i = 0; i < 59; i++)
cout << "-";
cout << endl;
}
int GetNumOfFunction() {
char NumOfFunction = 'a';
cout << "Enter function number: ";
cin >> NumOfFunction;
cin.ignore(1024, '\n');
while (int(NumOfFunction) < 49 || int(NumOfFunction) > 54) {
cout << "ERROR, enter function number: ";
cin >> NumOfFunction;
cin.ignore(1024, '\n');
}
return int(NumOfFunction) - 48;
}
void PrintAllDatas(vector <Empoloyee> AllEmployees, int& NumOfNotes) {
PrintTableForNotes;
for (int i = 0; i < NumOfNotes; i++)
PrintNote(AllEmployees[i]);
system("pause");
}
void FindEmployee(vector <Empoloyee> AllEmployees, int& NumOfNotes) {
char UsersSecondName[MaxBuffSize];
cout << "Enter employee's seconld name: ";
gets_s(UsersSecondName);
bool is_Find = false;
int NumOfNote = 0;
while (NumOfNote < NumOfNotes && !is_Find) {
if (!strcmp(UsersSecondName, AllEmployees[NumOfNote].SecondName)) {
PrintTableForNotes;
PrintNote(AllEmployees[NumOfNote]);
is_Find = true;
}
NumOfNote++;
}
if (!is_Find)
cout << "No employees with this second name:(" << endl;
system("pause");
}
void FilterByDepartment(vector <Empoloyee> AllEmployees, int& NumOfNotes) {
int UsersNumOfDepartment;
bool is_Find = false;
cout << "Enter number of department: ";
cin >> UsersNumOfDepartment;
for (int i = 0; i < NumOfNotes; i++) {
if (AllEmployees[i].NumOfDepartment == UsersNumOfDepartment && !is_Find) {
is_Find = true;
PrintTableForNotes();
PrintNote(AllEmployees[i]);
}
else if (AllEmployees[i].NumOfDepartment == UsersNumOfDepartment && is_Find)
PrintNote(AllEmployees[i]);
}
if (!is_Find)
cout << "You have no employees in this department" << endl;
system("pause");
}
void SortBySalary(vector <Empoloyee>& AllEmployees, int& NumOfNotes) {
for (int i = 1; i < NumOfNotes; ++i) {
for (int j = 0; j < NumOfNotes - i; j++) {
if (AllEmployees[j].Salary < AllEmployees[j + 1].Salary) {
Empoloyee TempNote = AllEmployees[j];
AllEmployees[j] = AllEmployees[j + 1];
AllEmployees[j + 1] = TempNote;
}
}
}
PrintAllDatas(AllEmployees, NumOfNotes);
}
void SaveDatabase(vector <Empoloyee>& AllEmployees, int& NumOfNotes) {
ofstream fout("Database.txt");
fout << NumOfNotes << endl;
for (int i = 0; i < NumOfNotes; i++) {
fout << AllEmployees[i].SecondName << endl;
fout << AllEmployees[i].Name << endl;
fout << AllEmployees[i].NumOfDepartment << endl;
fout << AllEmployees[i].Salary << endl;
delete[] AllEmployees[i].SecondName;
delete[] AllEmployees[i].Name;
}
AllEmployees.clear();
fout.close();
}
void PrintTableForNotes() {
cout << setw(15) << " Second Name |" << setw(15) << " Name |"
<< setw(5) << " NoD |" << setw(8) << " Salary" << endl;
}
void PrintNote(Empoloyee OneNote)
{
cout << setw(16) << OneNote.SecondName
<< setw(15) << OneNote.Name
<< setw(6) << OneNote.NumOfDepartment
<< setw(8) << OneNote.Salary << endl;
}


