Очищается массив при читке файла
hospital.h
#ifndef HOSPITALREGISTRY_HOSPITAL_H
#define HOSPITALREGISTRY_HOSPITAL_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Patient {
private:
string name;
string surname;
string phone;
int age;
protected:
int patientCode;
public:
void SetPatientName(string name);
void SetPatientSurname(string surname);
void SetPatientPhone(string phone);
void SetPatientAge(int age);
void SetPatientCode(int patientCode);
string GetPatientName();
string GetPatientSurname();
string GetPatientPhone();
int GetPatientAge();
int GetPatientCode();
};
class Doctor {
private:
string name;
string surname;
string qualification;
protected:
int doctorCode;
public:
Doctor() {
name = "";
surname = "";
qualification = "";
doctorCode = 0;
};
Doctor(string name, string surname, string qualification, int doctorCode) {
this->name = name;
this->surname = surname;
this->qualification = qualification;
this->doctorCode = doctorCode;
}
void SetDoctorName(string name);
void SetDoctorSurname(string surname);
void SetDoctorQualification(string qualification);
void SetDoctorCode(int doctorCode);
string GetDoctorName();
string GetDoctorSurname();
string GetDoctorQualification();
int GetDoctorCode();
};
class Registry : Patient, Doctor {
private:
string info;
struct date {
int day;
int month;
int year;
};
date date;
public:
Patient NewPatient();
void AddPatient(Patient *&patientsArray, int &numberOfPatients);
Registry NewAppointment(Patient *patientsArray, Doctor *doctorsArray, int numberOfDoctors, int numberOfPatients);
void AddPatientAppointment(Registry *&appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors);
void RemovePatientAppointment(Registry *appointmentsArray, int numberOfAllPatients);
void EditPatientAppointment(Registry *appointmentsArray, int numberOfAllPatients);
void ShowAllPatientAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients);
void ShowDoctorAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors);
};
class File {
private:
string extension = ".txt";
string patientsFileName = "patients";
string numberOfPatientsFileName = "numberOfPatients";
string doctorsFileName = "doctors";
string numberOfDoctorsFilename = "numberOfDoctors";
string appointmentsFileName = "appointments";
string patientsFilePath =
"/Users/otkisyan/CLionProjects/HospitalRegistry/cmake-build-debug/userfiles/" + patientsFileName +
extension;
string numberOfPatientsFilePath =
"/Users/otkisyan/CLionProjects/HospitalRegistry/cmake-build-debug/userfiles/" + numberOfPatientsFileName +
extension;
string doctorsFilePath =
"/Users/otkisyan/CLionProjects/HospitalRegistry/cmake-build-debug/userfiles/" + doctorsFileName + extension;
string numberOfDoctorsFilePath =
"/Users/otkisyan/CLionProjects/HospitalRegistry/cmake-build-debug/userfiles/" + numberOfDoctorsFilename +
extension;
string appointmentsFilePath =
"/Users/otkisyan/CLionProjects/HospitalRegistry/cmake-build-debug/userfiles/" + appointmentsFileName +
extension;
public:
void CheckFileExists();
void CreateFile();
void DeleteFile();
void ReadFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray);
void RewriteFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray);
};
#endif //HOSPITALREGISTRY_HOSPITAL_H
registry-class-implement.cpp
#include "hospital.h"
Patient Registry::NewPatient() {
Patient newPatient;
string name;
string surname;
string phone;
int age;
cout << "Введіть ім'я клієнта: " << endl;
cout << "➡ ";
cin >> name;
newPatient.SetPatientName(name);
cout << "Введіть фамілію клієнта: " << endl;
cout << "➡ ";
cin >> surname;
newPatient.SetPatientSurname(surname);
cout << "Введіть номер телефону клієнта: " << endl;
cout << "➡ ";
cin >> phone;
newPatient.SetPatientPhone(phone);
cout << "Введіть вік клієнта" << endl;
cout << "➡ ";
cin >> age;
newPatient.SetPatientAge(age);
return newPatient;
}
void Registry::AddPatient(Patient *&patientsArray, int &numberOfPatients) {
Patient *patientsArrayTemp = new Patient[numberOfPatients + 1];
for (int j = 0; j < numberOfPatients; j++) {
patientsArrayTemp[j] = patientsArray[j];
}
delete[] patientsArray;
patientsArray = patientsArrayTemp;
numberOfPatients++;
int i = numberOfPatients - 1;
patientsArray[i] = NewPatient();
patientsArray[i].SetPatientCode(i);
}
Registry
Registry::NewAppointment(Patient *patientsArray, Doctor *doctorsArray, int numberOfDoctors, int numberOfPatients) {
Registry newAppointment;
cout << "Введіть скаргу клієнта" << endl;
cout << "➡ ";
cin >> newAppointment.info;
cout << "Введіть дату прийому: " << endl;
cout << "День (1-31): " << endl;
cout << "➡ ";
cin >> newAppointment.date.day;
cout << "Місяць: " << endl;
cout << "➡ ";
cin >> newAppointment.date.month;
cout << "Рік: " << endl;
cout << "➡ ";
cin >> newAppointment.date.year;
cout << "Оберіть номер необхідного лікаря" << endl;
for (int i = 0; i < numberOfDoctors; i++) {
cout << "Номер лікаря: " << i << endl;
cout << "Ім'я лікаря: " << doctorsArray[i].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[i].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[i].GetDoctorQualification() << endl;
cout << endl;
}
cout << "➡ ";
int choosedDoctor = 0;
cin >> choosedDoctor;
newAppointment.doctorCode = choosedDoctor;
newAppointment.patientCode = patientsArray[numberOfPatients - 1].GetPatientCode();
return newAppointment;
}
void Registry::AddPatientAppointment(Registry *&appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors) {
Registry *appointmentsArrayTemp = new Registry[numberOfPatients + 1];
for (int j = 0; j < numberOfPatients; j++) {
appointmentsArrayTemp[j] = appointmentsArray[j];
}
delete[] appointmentsArray;
appointmentsArray = appointmentsArrayTemp;
int i = numberOfPatients - 1;
appointmentsArray[i] = appointmentsArray->NewAppointment(patientsArray, doctorsArray, numberOfDoctors,
numberOfPatients);
}
void Registry::ShowAllPatientAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients) {
for (int i = 0; i < numberOfPatients; i++) {
cout << endl;
cout << "Номер пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientCode() << endl;
cout << "Ім'я пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientName() << endl;
cout << "Фамілія пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientSurname() << endl;
cout << "Вік пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientAge() << endl;
cout << "Номер телефону пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientPhone()
<< endl;
cout << endl;
cout << "Назначений лікарь: " << endl;
cout << "Ім'я лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[appointmentsArray[i].doctorCode].GetDoctorQualification()
<< endl;
cout << "Дата прийому: " << appointmentsArray[appointmentsArray[i].patientCode].date.day << "."
<< appointmentsArray[appointmentsArray[i].patientCode].date.month
<< "." << appointmentsArray[appointmentsArray[i].patientCode].date.month << endl;
cout << "Скарги пацієнта: " << appointmentsArray[appointmentsArray[i].patientCode].info << endl;
}
}
void Registry::ShowDoctorAppointments(Registry *appointmentsArray, Patient *patientsArray, Doctor *doctorsArray,
int numberOfPatients, int numberOfDoctors) {
cout << "Оберіть номер необхідного лікаря" << endl;
for (int i = 0; i < numberOfDoctors; i++) {
cout << "Номер лікаря: " << i << endl;
cout << "Ім'я лікаря: " << doctorsArray[i].GetDoctorName() << endl;
cout << "Фамілія лікаря: " << doctorsArray[i].GetDoctorSurname() << endl;
cout << "Кваліфікація лікаря: " << doctorsArray[i].GetDoctorQualification() << endl;
cout << endl;
}
cout << "➡ ";
int choosedDoctor = 0;
cin >> choosedDoctor;
for (int i = 0; i < numberOfPatients; i++) {
if (doctorsArray[appointmentsArray[i].doctorCode].GetDoctorCode() == choosedDoctor) {
cout << "Номер пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientCode() << endl;
cout << "Ім'я пацієнта: " << patientsArray[appointmentsArray[i].patientCode].GetPatientName()
<< endl;
cout << "Фамілія пацієнта: "
<< patientsArray[appointmentsArray[i].patientCode].GetPatientSurname() << endl;
cout << "Вік пацієнта: " << patientsArray[appointmentsArray[choosedDoctor].patientCode].GetPatientAge()
<< endl;
cout << "Номер телефону пацієнта: "
<< patientsArray[appointmentsArray[i].patientCode].GetPatientPhone() << endl;
cout << "Дата прийому: " << appointmentsArray[appointmentsArray[i].patientCode].date.day << "."
<< appointmentsArray[appointmentsArray[i].patientCode].date.month
<< "." << appointmentsArray[appointmentsArray[i].patientCode].date.month << endl;
cout << endl;
}
}
}
file-class-impelement.cpp
#include "hospital.h"
void File::CheckFileExists() {
ifstream patientsFile;
ifstream numberOfPatientsFile;
ifstream doctorsFile;
ifstream numberOfDoctorsFile;
ifstream appointmentsFile;
bool patientsFileExists = false;
bool numberOfPatientsFileExists = false;
bool doctorsFileExists = false;
bool numberOfDoctorsFileExists = false;
bool appointmentsFileExists = false;
patientsFile.open(patientsFilePath);
//data.open(dataFilePath);
if (patientsFile) {
patientsFileExists = true;
patientsFile.close();
}
numberOfPatientsFile.open(numberOfPatientsFilePath);
if (numberOfPatientsFile) {
numberOfPatientsFileExists = true;
numberOfPatientsFile.close();
}
doctorsFile.open(doctorsFilePath);
if (doctorsFile) {
doctorsFileExists = true;
doctorsFile.close();
}
numberOfDoctorsFile.open(numberOfDoctorsFilePath);
if (numberOfDoctorsFile) {
numberOfDoctorsFileExists = true;
numberOfDoctorsFile.close();
}
appointmentsFile.open(appointmentsFilePath);
if (appointmentsFile) {
appointmentsFileExists = true;
appointmentsFile.close();
}
if (patientsFileExists == false && numberOfPatientsFileExists == false && doctorsFileExists == false &&
numberOfDoctorsFileExists == false && appointmentsFileExists == false) {
CreateFile();
}
}
void File::CreateFile() {
ifstream patientsFile;
ifstream numberOfPatientsFile;
ifstream doctorsFile;
ifstream numberOfDoctorsFile;
ifstream appointmentsFile;
patientsFile.open(patientsFilePath.c_str(), ios::binary | ios::out);
patientsFile.close();
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str(), ios::binary | ios::out);
numberOfPatientsFile.close();
doctorsFile.open(doctorsFilePath.c_str(), ios::binary | ios::out);
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str(), ios::binary | ios::out);
appointmentsFile.open(appointmentsFilePath.c_str(), ios::binary | ios::out);
}
void File::DeleteFile() {
remove(patientsFilePath.c_str());
remove(numberOfPatientsFilePath.c_str());
remove(doctorsFilePath.c_str());
remove(numberOfDoctorsFilePath.c_str());
remove(appointmentsFilePath.c_str());
}
void File::ReadFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray) {
ifstream patientsFile;
ifstream numberOfPatientsFile;
ifstream doctorsFile;
ifstream numberOfDoctorsFile;
ifstream appointmentsFile;
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str(), ios::binary);
numberOfPatientsFile.read((char *) &numberOfPatients, sizeof(numberOfPatients));
numberOfPatientsFile.close();
patientsFile.open(patientsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfPatients; i++) {
patientsFile.read((char *) &patientsArray[i], sizeof(patientsArray[i]));
}
patientsFile.close();
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str(), ios::binary);
numberOfDoctorsFile.read((char *) &numberOfDoctors, sizeof(numberOfDoctors));
numberOfDoctorsFile.close();
doctorsFile.open(doctorsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfDoctors; i++) {
doctorsFile.read((char *) &doctorsArray[i], sizeof(doctorsArray[i]));
}
doctorsFile.close();
appointmentsFile.open(appointmentsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfPatients; i++) {
appointmentsFile.read((char *) &appointmentsArray[i], sizeof(appointmentsArray[i]));
//когда доходит до сюда, массив patientsArray очищается под ноль
}
appointmentsFile.close();
}
void File::RewriteFile(Patient *patientsArray, int &numberOfPatients, Doctor *doctorsArray, int &numberOfDoctors,
Registry *appointmentsArray) {
ofstream patientsFile;
ofstream numberOfPatientsFile;
ofstream doctorsFile;
ofstream numberOfDoctorsFile;
ofstream appointmentsFile;
numberOfPatientsFile.open(numberOfPatientsFilePath.c_str(), ios::binary);
numberOfPatientsFile.write((char *) &numberOfPatients, sizeof(numberOfPatients));
numberOfPatientsFile.close();
patientsFile.open(patientsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfPatients; i++) {
patientsFile.write((char *) &patientsArray[i], sizeof(patientsArray[i]));
}
patientsFile.close();
numberOfDoctorsFile.open(numberOfDoctorsFilePath.c_str(), ios::binary);
numberOfDoctorsFile.write((char *) &numberOfDoctors, sizeof(numberOfDoctors));
numberOfDoctorsFile.close();
doctorsFile.open(doctorsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfDoctors; i++) {
doctorsFile.write((char *) &doctorsArray[i], sizeof(doctorsArray[i]));
}
doctorsFile.close();
appointmentsFile.open(appointmentsFilePath.c_str(), ios::binary);
for (int i = 0; i < numberOfPatients; i++) {
appointmentsFile.write((char *) &appointmentsArray[i], sizeof(appointmentsArray[i]));
}
appointmentsFile.close();
}
main.cpp
#include "hospital.h"
int main() {
int numberOfPatients = 0;
int numberOfDoctors = 2;
File file;
Patient *patientsArray = new Patient[numberOfPatients];
Registry *appointmentsArray = new Registry[numberOfPatients];
Doctor *doctorsArray = new Doctor[numberOfDoctors]{{"Егор", "Родионов", "Аллерголог", 0},
{"Ярик", "Бачок", "kek", 1}
};
file.CheckFileExists();
int use = 0;
int work = 0;
do {
cin >> use;
switch (use) {
case 1: {
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->AddPatient(patientsArray, numberOfPatients);
appointmentsArray->AddPatientAppointment(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients, numberOfDoctors);
file.RewriteFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
cin >> work;
break;
}
case 2: {
work = 1;
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->ShowAllPatientAppointments(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients);
cout << "Бажаєте продовжити роботу? 1 - так, 2 - ні" << endl;
cout << "➡ ";
cin >> work;
break;
}
case 3: {
file.ReadFile(patientsArray, numberOfPatients, doctorsArray, numberOfDoctors, appointmentsArray);
appointmentsArray->ShowDoctorAppointments(appointmentsArray, patientsArray, doctorsArray,
numberOfPatients, numberOfDoctors);
}
}
} while (work == 1);
return 0;
}
Всем привет, пишу типо "регистратуру для больницы". Но возникла проблема, когда читаю файл в функции void File::ReadFile() и дохожу до читки массива appointmentsArray, то массив patientsArray полностью очищается. Я отметил это комментарием в коде (файл file-class-implement.cpp). Я не понимаю как такое возможно, как другой массив влияет на тот. Прошу помощи!