I'm writing a programming lab and I have this error, I don't know how to solve this, please help bug the program crashes when I select item 10 and information from the file is not deleted afterward
Also, the program doesn't delete information from file
My mentor said that I must delete string and write cod without them, but tasks if the lab require to use string
help me, please
there is my code
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
const int MAX_LENGTH = 100;
using namespace std;
enum Options {
EXIT,
GET_COUNT,
GET_DRIVERS,
GET_ADDRESSES,
SET_DRIVERS,
SET_ADDRESSES,
INPUT,
OUTPUT,
FILE_OUTPUT,
FILE_INPUT,
PRINT_FILE,
FREE_TAKEN_RATIO,
DRIVER_BY_ADDRESS,
HAS_DRIVER_ADDRESS,
CHECK_COPY_TAXI,
CHECK_DYNAMIC_TAXI
};
class Taxi {
int drivers[MAX_LENGTH]; // Масив водіїв
int driversLength;
static int count; // Статичне поле для відстеження кількості таксі
protected:
string addresses[MAX_LENGTH];
int addressesLength;
public:
string passenger; // Пасажир
Taxi(); // Конструктори
Taxi(int*, int, string*, int, string);
Taxi(Taxi&);
~Taxi(); // Деструктор
void getDrivers(); // Методи для отримання і встановлення даних про водіїв та адреси
void getAddresses();
void setDrivers(int*, int);
void setAddresses(string*, int);
void input();
void output();
void fileOutput(string);
void fileInput(string);
bool order(int, string);
int order(string);
double order();
static int getCount() { return count; }// Статичний метод для отримання кількості таксі
};
int getNumber(string);
void fillArrayOfDrivers(int*, int);
void fillArrayOfAddresses(string*, int);
void fileOutputAll(string);
void checkCopyTaxi(Taxi&);
void checkDynamicTaxi();
int menu();
bool Taxi::order(int id, string addr) {
if (drivers[id] == 1)
return true;
return false;
}
int Taxi::order(string addr) {
for (int i = 0; i < driversLength; i++)
if (drivers[i] == 1) {
drivers[i] = 0;
return i + 1;
}
return 0;
}
double Taxi::order() {
int free = 0;
int taken = 0;
for (int i = 0; i < driversLength; i++)
if (drivers[i])
free++;
else
taken++;
if (taken == 0)
return -1;
else
return (double)free / taken;
}
// Оголошення статичної змінної
int Taxi::count = 0;
// Опис конструкторів та деструктора
Taxi::Taxi() {
driversLength = 2;
for (int i = 0; i < driversLength; i++)
drivers[i] = 1;
addressesLength = 3;
for (int i = 0; i < addressesLength; i++)
addresses[i] = "street123";
passenger = "Johnny";
cout << "Taxi by default is created!\n";
count++;
}
Taxi::Taxi(int* dr, int drLength, string* addr, int addrLength, string pas) {
driversLength = drLength;
for (int i = 0; i < driversLength; i++)
drivers[i] = dr[i];
addressesLength = addrLength;
for (int i = 0; i < addressesLength; i++)
addresses[i] = addr[i];
passenger = pas;
cout << "Taxi by parameters is created!\n";
count++;
}
Taxi::Taxi(Taxi& t) {
driversLength = t.driversLength;
for (int i = 0; i < driversLength; i++)
drivers[i] = t.drivers[i];
addressesLength = t.addressesLength;
for (int i = 0; i < t.addressesLength; i++)
addresses[i] = t.addresses[i];
passenger = t.passenger;
cout << "Taxi by copy is created!\n";
count++;
}
Taxi::~Taxi() {
cout << "Taxi is now deleted!\n";
count--;
}
void Taxi::input() {
driversLength = getNumber("Enter drivers length: ");
fillArrayOfDrivers(drivers, driversLength);
addressesLength = getNumber("Enter addresses length: ");
fillArrayOfAddresses(addresses, addressesLength);
cout << "Enter passenger name: ";
getline(cin, passenger);
cout << "\n";
}
void Taxi::output() {
cout << "Drivers: " << setw(22) << "Addresses: " << setw(22)
<< "Passenger: " << endl;
int i = 0;
while ((i < addressesLength && i < driversLength) || i == 0) {
if (drivers[i] == 1)
cout << "Driver " << i + 1 << " is free";
else
cout << "Driver " << i + 1 << " is taken";
cout << setw(15) << addresses[i];
if (i == 0)
cout << setw(15) << passenger;
cout << endl;
i++;
}
while (i < driversLength) {
if (drivers[i] == 1)
cout << "Driver " << i + 1 << " is free\n";
else
cout << "Driver " << i + 1 << " is taken\n";
i++;
}
while (i < addressesLength) {
cout << setw(31) << addresses[i] << "\n";
i++;
}
}
int getNumber(string msg) {// Функція для отримання числа від користувача
cout << msg;
char number[11];
cin.getline(number, 11);
return atoi(number);
}
void fillArrayOfDrivers(int* array, int length) {// Функція для заповнення масиву водіїв
if (length == 0)
return;
cout << "Enter drivers: \n";
for (int i = 0; i < length; i++) {
char answer;
cout << "Is driver " << i + 1 << " free?(y/n) ";
cin >> answer;
if (answer == 'y')
array[i] = 1;
else
array[i] = 0;
}
cin.ignore();
cout << "\n";
}
void fillArrayOfAddresses(string* array, int length) {
if (length == 0)
return;
cout << "Enter addresses: \n";
for (int i = 0; i < length; i++) {
cout << "Enter address " << i + 1 << ": ";
getline(cin, array[i]);
}
cout << "\n";
}
void checkCopyTaxi(Taxi& chosenTaxi) {// Функція для перевірки роботи програми з копією таксі
Taxi copyTaxi = chosenTaxi;
int dr[MAX_LENGTH];
string addr[MAX_LENGTH];
int addrLength, drLength, count;
double ratio;
int driverId, chosenDriverId;
string chosenAddress;
string filename;
cout << "Set drivers of copy taxi: \n";
drLength = getNumber("Enter drivers length: ");
fillArrayOfDrivers(dr, drLength);
copyTaxi.setDrivers(dr, drLength);
cout << endl;
cout << "Set addresses of copy taxi: \n";
addrLength = getNumber("Enter addresses length: ");
fillArrayOfAddresses(addr, addrLength);
copyTaxi.setAddresses(addr, addrLength);
cout << endl;
cout << "Get drivers of copy taxi: \n";
copyTaxi.getDrivers();
cout << endl;
cout << "Get addresses of copy taxi: \n";
copyTaxi.getAddresses();
cout << endl;
cout << "Input copy taxi: \n";
copyTaxi.input();
cout << endl;
cout << "Output copy taxi: \n";
copyTaxi.output();
cout << endl;
cout << "Output copy taxi to file: \n";
cout << "Enter filename: ";
getline(cin, filename);
copyTaxi.fileOutput(filename);
cout << endl;
cout << "Input copy taxi from file: \n";
cout << "Enter filename: ";
getline(cin, filename);
copyTaxi.fileInput(filename);
cout << endl;
cout << "Get free/taken ratio for copy taxi: \n";
ratio = copyTaxi.order();
if (ratio == -1)
cout << "All drivers are free" << endl;
else
cout << "Free/taken ratio: " << ratio << endl;
cout << endl;
cout << "Get driver by address for copy taxi: \n";
cout << "Enter an address: ";
getline(cin, chosenAddress);
driverId = copyTaxi.order(chosenAddress);
if (!driverId)
cout << "No drivers by address " << chosenAddress << endl;
else
cout << "Driver's id: " << driverId << endl;
cout << endl;
cout << "Check if chosen driver with chosen address is free for copy "
"taxi: \n";
chosenDriverId = getNumber("Enter id of chosen driver: ");
cout << "Enter an address: ";
getline(cin, chosenAddress);
if (copyTaxi.order(chosenDriverId, chosenAddress))
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is free\n";
else
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is taken/not found\n";
cout << endl;
}
void checkDynamicTaxi() { // Функція для перевірки роботи програми з динамічно створеним таксі
Taxi* dynamicTaxi = new Taxi;
int dr[MAX_LENGTH];
string addr[MAX_LENGTH];
int addrLength, drLength, count;
double ratio;
int driverId, chosenDriverId;
string chosenAddress;
string filename;
cout << "Set drivers of dynamically created taxi: \n";
drLength = getNumber("Enter drivers length: ");
fillArrayOfDrivers(dr, drLength);
dynamicTaxi->setDrivers(dr, drLength);
cout << endl;
cout << "Set addresses of dynamically created taxi: \n";
addrLength = getNumber("Enter addresses length: ");
fillArrayOfAddresses(addr, addrLength);
dynamicTaxi->setAddresses(addr, addrLength);
cout << endl;
cout << "Get drivers of dynamically created taxi: \n";
dynamicTaxi->getDrivers();
cout << endl;
cout << "Get addresses of dynamically created taxi: \n";
dynamicTaxi->getAddresses();
cout << endl;
cout << "Input dynamically created taxi: \n";
dynamicTaxi->input();
cout << endl;
cout << "Output dynamically created taxi: \n";
dynamicTaxi->output();
cout << endl;
cout << "Output dynamically created taxi to file: \n";
cout << "Enter filename: ";
getline(cin, filename);
dynamicTaxi->fileOutput(filename);
cout << endl;
cout << "Input dynamically created taxi from file: \n";
cout << "Enter filename: ";
getline(cin, filename);
dynamicTaxi->fileInput(filename);
cout << endl;
cout << "Get free/taken ratio for dynamically created taxi: \n";
ratio = dynamicTaxi->order();
if (ratio == -1)
cout << "All drivers are free" << endl;
else
cout << "Free/taken ratio: " << ratio << endl;
cout << endl;
cout << "Get driver by address for dynamically created taxi: \n";
cout << "Enter an address: ";
getline(cin, chosenAddress);
driverId = dynamicTaxi->order(chosenAddress);
if (!driverId)
cout << "No drivers by address " << chosenAddress << endl;
else
cout << "Driver's id: " << driverId << endl;
cout << endl;
cout << "Check if chosen driver with chosen address is free for dynamically "
"created "
"taxi: \n";
chosenDriverId = getNumber("Enter id of chosen driver: ");
cout << "Enter an address: ";
getline(cin, chosenAddress);
if (dynamicTaxi->order(chosenDriverId, chosenAddress))
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is free\n";
else
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is taken/not found\n";
cout << endl;
delete dynamicTaxi;
}
int menu() {
cout << "\n"
<< EXIT << ") Exit\n"
<< GET_COUNT << ") Get count\n"
<< GET_DRIVERS << ") Get drivers\n"
<< GET_ADDRESSES << ") Get addresses\n"
<< SET_DRIVERS << ") Set drivers\n"
<< SET_ADDRESSES << ") Set addresses\n"
<< INPUT << ") Input new taxi\n"
<< OUTPUT << ") Output taxi\n"
<< FILE_OUTPUT << ") Output taxi to file\n"
<< FILE_INPUT << ") Input taxi from file\n"
<< PRINT_FILE << ") Print contents of file\n"
<< FREE_TAKEN_RATIO << ") Get free/taken ratio\n"
<< DRIVER_BY_ADDRESS << ") Get driver by address\n"
<< HAS_DRIVER_ADDRESS
<< ") Check if chosen driver with chosen address is free\n"
<< CHECK_COPY_TAXI << ") Check if program works with copy taxi\n"
<< CHECK_DYNAMIC_TAXI << ") Check if program works with dynamic taxi\n";
int option = getNumber("What option? ");
cout << "\n";
return option;
}
void Taxi::getDrivers() {// Метод для виведення водіїв таксі
if (driversLength == 0) {
cout << "This taxi does not have any drivers yet\n";
return;
}
cout << "Drivers: \n";
for (int i = 0; i < driversLength; i++)
if (drivers[i] == 1)
cout << "Driver " << i << " is free\n";
else
cout << "Driver " << i << " is taken\n";
}
void Taxi::getAddresses() {// Метод для виведення адрес таксі
if (addressesLength == 0) {
cout << "This taxi does not have any addresses yet\n";
return;
}
cout << "Addresses: \n";
for (int i = 0; i < addressesLength; i++)
cout << addresses[i] << endl;
}
void Taxi::setDrivers(int* dr, int drLength) { // Метод для встановлення водіїв таксі
driversLength = drLength;
for (int i = 0; i < driversLength; i++)
drivers[i] = dr[i];
cout << "Drivers are set!\n";
}
void Taxi::setAddresses(string* addr, int addrLength) { // Метод для встановлення адрес таксі
addressesLength = addrLength;
for (int i = 0; i < addressesLength; i++)
addresses[i] = addr[i];
cout << "Adddresses are set!\n";
}
void Taxi::fileInput(string filename) {// Метод для введення даних таксі з файлу
ifstream fileRead(filename, ios::binary);
if (!fileRead.is_open()) {
cout << "Cannot open " << filename << endl;
return;
}
fileRead.seekg(0, ios::end);
int fileSize = fileRead.tellg();
if (fileSize == 0) {
cout << filename << " is empty\n";
fileRead.close();
return;
}
fileRead.seekg(fileSize - sizeof(*this), ios::beg);
fileRead.read((char*)this, sizeof(*this));
cout << "Taxi is succesfully inputted from " << filename << "\n";
fileRead.close();
}
void Taxi::fileOutput(string filename) {// Метод для виведення даних таксі у файл
ofstream fileWrite(filename, ios::app | ios::binary);
if (!fileWrite.is_open()) {
cout << "Cannot open " << filename << " for an output\n";
return;
}
fileWrite.write((char*)this, sizeof(*this));
cout << "Taxi is succesfully outputted to " << filename << "\n";
fileWrite.close();
}
void fileOutputAll(string filename) { // Функція для виведення всіх таксі з файлу
ifstream fileRead(filename, ios::binary);
if (!fileRead.is_open()) {
cout << "Cannot open " << filename << endl;
return;
}
fileRead.seekg(0, ios::end);
int fileSize = fileRead.tellg();
if (fileSize == 0) {
cout << filename << " is empty\n";
fileRead.close();
return;
}
fileRead.seekg(0, ios::beg);
while (!fileRead.eof()) {
Taxi myTaxi;
fileRead.read((char*)&myTaxi, sizeof(myTaxi));
//myTaxi.output();
cout << endl;
}
fileRead.close();
cout << "finish";
}
int main() {
cout << "Count: " << Taxi::getCount() << endl;
Taxi chosenTaxi;
int dr[MAX_LENGTH];
string addr[MAX_LENGTH];
int addrLength, drLength, count;
double ratio;
int driverId, chosenDriverId;
string chosenAddress;
string filename;
int option;
do {
option = menu();
switch (option) {
case GET_COUNT:
count = Taxi::getCount();
cout << "Count: " << count << endl;
break;
case GET_DRIVERS:
chosenTaxi.getDrivers();
break;
case GET_ADDRESSES:
chosenTaxi.getAddresses();
break;
case SET_DRIVERS:
drLength = getNumber("Enter drivers length: ");
fillArrayOfDrivers(dr, drLength);
chosenTaxi.setDrivers(dr, drLength);
break;
case SET_ADDRESSES:
addrLength = getNumber("Enter addresses length: ");
fillArrayOfAddresses(addr, addrLength);
chosenTaxi.setAddresses(addr, addrLength);
break;
case INPUT:
chosenTaxi.input();
break;
case OUTPUT:
cout << "Chosen taxi: \n";
chosenTaxi.output();
break;
case FILE_OUTPUT:
cout << "Enter filename: ";
getline(cin, filename);
chosenTaxi.fileOutput(filename);
break;
case FILE_INPUT:
cout << "Enter filename: ";
getline(cin, filename);
chosenTaxi.fileInput(filename);
break;
case PRINT_FILE:
cout << "Enter filename: ";
getline(cin, filename);
fileOutputAll(filename);
break;
case FREE_TAKEN_RATIO:
ratio = chosenTaxi.order();
if (ratio == -1)
cout << "All drivers are free" << endl;
else
cout << "Free/taken ratio: " << ratio << endl;
break;
case DRIVER_BY_ADDRESS:
cout << "Enter an address: ";
getline(cin, chosenAddress);
driverId = chosenTaxi.order(chosenAddress);
if (!driverId)
cout << "No drivers by address " << chosenAddress << endl;
else
cout << "Driver's id: " << driverId << endl;
break;
case HAS_DRIVER_ADDRESS:
chosenDriverId = getNumber("Enter id of chosen driver: ");
cout << "Enter an address: ";
getline(cin, chosenAddress);
if (chosenTaxi.order(chosenDriverId, chosenAddress))
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is free\n";
else
cout << "Driver " << chosenDriverId << " with address " << chosenAddress
<< " is taken/not found\n";
break;
case CHECK_COPY_TAXI:
checkCopyTaxi(chosenTaxi);
break;
case CHECK_DYNAMIC_TAXI:
checkDynamicTaxi();
break;
case EXIT:
cout << "End of program\n\n";
break;
default:
cout << "Choose valid option\n";
break;
}
} while (option != EXIT);
return 0;
}