Не получается сохранить информацию в файл sum.txt в с++
в коде работают все файлы кроме:"sum.txt" и "profit.txt".Проблема в том,что в них хранится информация из вектора,но при закрытии программы файл обнуляется.Ниже сам код.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Human
{
protected:
string initials;
string gender;
int age;
};
class Worker :Human
{
protected:
string specialization;
float salary;
int shift_number;
string post;
};
class Accountant :Worker
{
protected:
ofstream fout;
fstream fout_1;
ofstream profit;
fstream profit_1;
protected:
vector<int>Sum_cost;
vector<int>Sum_profit;
ofstream Sum_Cost;
ifstream Sum_Cost_1;
ofstream Sum_Profit;
ifstream Sum_Profit_1;
public:
Accountant() {
fout.open("costCalculation.txt", ios::app);
if (!fout.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
fout_1.open("costCalculation.txt");
if (!fout_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
profit.open("profitCalculation.txt",ios::app);
if (!profit.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
profit_1.open("profitCalculation.txt");
if (!profit_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Cost.open("sum.txt");
if (!Sum_Cost.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Profit.open("profit.txt");
if (!Sum_Profit.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Profit_1.open("profit.txt", ios::app);
if (!Sum_Profit_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Cost_1.open("sum.txt", ios::app);
if (!Sum_Cost_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
}
~Accountant() {
if (fout.is_open()) {
fout.close();
}
if (fout_1.is_open()) {
fout_1.close();
}
if (profit.is_open()) {
profit.close();
}
if (profit_1.is_open()) {
profit_1.close();
}
if (Sum_Cost.is_open()) {
Sum_Cost.close();
}
if (Sum_Profit.is_open()) {
Sum_Profit.close();
}
if (Sum_Profit_1.is_open()) {
Sum_Profit_1.close();
}
if (Sum_Cost_1.is_open()) {
Sum_Cost_1.close();
}
}
void switch_accountant()
{
while (true) {
int choice;
cout << "Выберите варианте" << endl;
cout << "1. Работа с затратами" << endl;
cout << "2. Работа с прибылью" << endl;
cout << "0. Выход " << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
cost_calculation();
break;
case 2:
profit_calculation();
break;
}
}
}
void cost_calculation(){//-Подсчет затрат()
int choice;
int value;
int sum_cost = 0;
int cost;
while (true) {
cout << "Выберите варианте" << endl;
cout << "1. Есть новые затраты" << endl;
cout << "2. Нет новых затрат" << endl;
cout << "0. Выход " << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
cout << "Введите новые затраты" << endl;
cin >> cost;
fout << " " << cost;
while (!fout_1.eof()) {
fout_1 >> value;
sum_cost += value;
}
sum_cost = sum_cost + cost;
cout <<"Всего потратили:" << sum_cost << endl;
Sum_cost.push_back(sum_cost);
for (int value_sum : Sum_cost) {
Sum_Cost << value_sum << endl;
}
break;
case 2:
while (!fout_1.eof()) {
fout_1 >> value;
sum_cost += value;
}
cout <<"Всего потратили:" << sum_cost << endl;
Sum_cost.push_back(sum_cost);
for (int value_sum : Sum_cost) {
Sum_Cost << value_sum << endl;
}
break;
}
}
}
void profit_calculation() {//-Подсчет прибыли()
int value_1;
int cost_profit;
int sum_profit = 0;
cout << "Введите прибыль" << endl;
cin >> cost_profit;
profit << " " << cost_profit;
while (!profit_1.eof()) {
profit_1 >> value_1;
sum_profit += value_1;
}
sum_profit = sum_profit + cost_profit;
cout << "Всего заработали:" << sum_profit<< endl;
Sum_profit.push_back(sum_profit);
for (int value_sum : Sum_profit) {
Sum_Profit << value_sum << endl;
}
}
};
class Mechanic : public Worker {
private:
ofstream report;
fstream report_1;
string rep;
public:
Mechanic() {
report.open("mechanicReport.txt", ios::app);
if (!report.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
report_1.open("mechanicReport.txt");
if (!report_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
}
~Mechanic() {
if (report.is_open()) {
report.close();
}
if (report_1.is_open()) {
report_1.close();
}
}
void Make_report_work() {
string carBrand;
string repairType;
int repairTime;
while (true) {
int choice;
cout << "1. Выберите машину" << endl;
cout << "2. Выберите тип ремонта" << endl;
cout << "3. Укажите затраченное время" << endl;
cout << "0. Выход" << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
BrandSelection();
break;
case 2:
RepairTypeSelection();
break;
case 3:
RepairTime();
break;
}
}
}
void BrandSelection() {
while (true) {
int choice;
cout << "Выберите марку машины:" << endl;
cout << "1. Lada" << endl;
cout << "2. BMW" << endl;
cout << "3. Toyota" << endl;
cout << "4. Volkswagen" << endl;
cout << "5. Hyundai" << endl;
cout << "0. Выход" << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
rep = "Я чинил машину марки:Lada";
break;
case 2:
rep = "Я чинил машину марки:BMW";
break;
case 3:
rep = "Я чинил машину марки:Toyota";
break;
case 4:
rep = "Я чинил машину марки:Volkswagen";
break;
case 5:
rep = "Я чинил машину марки:Hyundai";
break;
}
}
report << rep;
}
void RepairTypeSelection() {
// Реализуйте выбор типа ремонта аналогичным образом
while (true) {
int choice;
cout << "Выберите тип ремонта:" << endl;
cout << "1. Замена масла" << endl;
cout << "2. Ремонт двигателя" << endl;
cout << "3. Замена калес" << endl;
cout << "4. Профилактический осмотр" << endl;
cout << "0. Выход" << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
rep = " Я произвел замену масла за";
break;
case 2:
rep = " Я произвел ремонт двигателя за";
break;
case 3:
rep = " Я произвел замену колес за";
break;
case 4:
rep = " Я совершил проф.осмотр за";
break;
}
}
report << rep;
}
void RepairTime() {
int repairTime;
cout << "Введите время затрат (в часах): ";
cin >> repairTime;
report <<" "<< repairTime;
}
void Reprot() {
int value;
while (!report_1.eof()) {
report_1 >> value;
}
}
};
class Manager:Accountant,Mechanic
{
private:
ifstream Sum_Profit_1;
ifstream Sum_Cost_1;
ofstream Sum_Cost;
ofstream Sum_Profit;
public:
Manager() {
Sum_Cost.open("sum.txt", ios::app);
if (!Sum_Cost.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Profit.open("profit.txt", ios::app);
if (!Sum_Profit.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Profit_1.open("profit.txt");
if (!Sum_Profit_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
Sum_Cost_1.open("sum.txt");
if (!Sum_Cost_1.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
}
~Manager() {
if (Sum_Cost.is_open()) {
Sum_Cost.close();
}
if (Sum_Profit.is_open()) {
Sum_Profit.close();
}
if (Sum_Profit_1.is_open()) {
Sum_Profit_1.close();
}
if (Sum_Cost_1.is_open()) {
Sum_Cost_1.close();
}
}
void switch_Manager()
{
while (true) {
int choice;
cout << "Выберите варианте" << endl;
cout << "1. Отчет Бугхалтерии" << endl;
cout << "2. Отчет Механиков" << endl;
cout << "0. Выход " << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
Result();
break;
case 2:
break;
}
}
}
void Result() {
string LastLine;
string lastline;
string CurrentLine;
string Currentline;
while (getline(Sum_Cost_1, CurrentLine)) {
LastLine = CurrentLine;
}
cout << "Всего затрат:" << LastLine << endl;;
while (getline(Sum_Profit_1, Currentline)) {
lastline = Currentline;
}
cout << "Всего Заработали:" << lastline << endl;
}
void information_client(){
}//-побаловаться с вектором и map
};
class Client :Human
{
protected:
int discount_card_number;
int phone_number;
private:
//float pay_order(){} Оплатить заказ()
};
class Automobile
{
private:
string brand;
string car_number;
int mass;
string body_type;
string year_release;
};
int main()
{
Mechanic mechanic;
Accountant accountant;
Manager manager;
int choice; // переменнай для выбора
setlocale(LC_ALL, "Russian");
while (true)
{
cout << "Выберите действие:" << endl;
cout << "1. Взаимодействовать с бугхалтерией" << endl;
cout << "2. Прочитать отчет менеджера" << endl;
cout << "3. Составить отчет механика " << endl;
cout << "4. Увидеть информацию о клиентах" << endl;
cout << "0. Выход " << endl;
cin >> choice;
if (choice == 0)
{
break;
}
switch (choice)
{
case 1:
accountant.switch_accountant();
break;
case 2:
manager.switch_Manager();
break;
case 3:
mechanic.Make_report_work();
break;
}
}
}
P.S. Заранее спасибо за помощь