Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 3836
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T03:52:42+00:00 2022-06-03T03:52:42+00:00

c++ – Error: libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc

  • 0

[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]

  • 0 0 Answers
  • 22 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8143 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1926 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1960 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.