[ad_1]
I’m getting this error… my code tries to simulate a Car Rental, in which, if you are a User, you are able to select a car after showing an array of Car Inventary. I have a base class named Car, and three inherited classes, Suv, AllTerrain and Luxury, which every type of car has its attributes inherited by the base class Car. In Inventary class, I created a pointer of type Car and with that an array max size. In the same class there’s a method, which I constructed some Cars depending on their characteristics: Suv, AllTerrain and Luxury, with newhttps://stackoverflow.com/q/72484512 (the logic I used to index every car was with an ID number, which helps iterate through every car).
The point, is that, I have a method called bookCars(), method that calls bool showByPrice() , which searches for a car given the users’ input (the main characteristics), if it matches, then I call delete in that index… At this point everything good.
After this, I want to show the user the current state of the Inventary, which, gets me the error, I thought the destructors weren’t being called after deleting the match car, but, they’re called… So, my plan is to immediately show the brand new Inventary of cars, but without the car that has been booked just before watch the first piece of code, where I call addCarsInventary(), then bookCars() finally where the error is: showInventary(), after deleting the object of that array supposely.
Here’s part of my code:
char s=" "; // Variable para hacer el menú
string optionCeo = " "; // Variable para hacer el menú
CarRental *Alamos = new CarRental(); // Apuntador de tipo CarRental para acceder con apuntadores
cout << "----------- Este es el menú de CarRental -----------" << endl
<< endl;
cout << " En este programa tienes dos opciones " << endl
<< endl;
cout << " Puedes ser CEO y agregar carros al inventario " << endl
<< " o cambiar el precio de un carro " << endl
<< endl;
cout << " O ser usuario y rentar un carro de los disponibles " << endl
<< endl;
cout << " ¡¡¡Por favor teclea muy bien a la hora de Reservar o ser CEO!!! " << endl
<< endl;
while (s != 'R' || s != 'C' || s != 'S')
{
cout << " Teclea R si quieres rentar, C si eres CEO o S para salir " << endl
<< endl;
cin >> s;
if (s == 'R')
{
cout << "Nota, a la hora de reservar carro se muy específico y teclea bien el carro que quieres" << endl
<< endl;
cout << "¡Escribiendo bien la marca, el rendimiento y el color por favor!" << endl
<< endl;
Alamos->addCarsInventary();
cout << endl
<< endl;
Alamos->bookCars();
Alamos->showInventary();// LINE WHERE I GET ERROR
Method that matches inputs
bool Inventary::showByPrice()
float perform;
string brand, color, type;
uint price, idDel, seats;
bool flag = false;
bool transm;
cout << "Estos son los datos de los carros disponibles para que puedas reservarlos: " << endl;
for (int i = 0; i < id; i++)
{
cout << "Precio: " << cars[i]->getPrice() << " color: " << cars[i]->getColor() << " marca: " << cars[i]->getBrand() << " rendimiento: " << cars[i]->getPerform() << " asientos: " << cars[i]->getSeats() << endl
<< endl;
}
cout << "Ahora por favor, escribe el precio del carro que quieres: " << endl;
cin >> price;
cout << "Ahora por favor, escribe el color del carro que quieres: " << endl;
cin >> color;
cout << "Ahora por favor, escribe la marca del carro que quieres: " << endl;
cin >> brand;
cout << "Ahora por favor, escribe el rendimiento del carro que quieres: " << endl;
cin >> perform;
for (int i = 0; i < id; i++)
{
if (cars[i]->getPrice() == price && cars[i]->getColor() == color && cars[i]->getBrand() == brand && cars[i]->getPerform() == perform)
{
Inventary::showCar(i);
idDel = Inventary::identifier(i);
Inventary::deleteCar(idDel);
flag = true;
}
}
if (flag == true)
{
return true;
}
else
return false;
Method that deletes car in carshttps://stackoverflow.com/q/72484512 position
void Inventary::deleteCar(uint iden)
for (int i = 0; i < id; i++)
{
if (cars[i]->getId() == iden)
{
delete cars[i];
}
}
id--;
[ad_2]