mardi 30 novembre 2021

Writing to existing Sales_data object in std::vector causes program to crash (C++)

Hope you're all doing well!

I'm reading and doing the exercises in Stanley Lippman's C++ Primer Ed.5.

I'm having the following issue with my code, when I update an existing Sales_data object in the std::vector<Sales_data> vec; the program crashes.

In order to over come this I erase the existing Sales_data object and replace it with a new updated object.

Is there a more efficient way of doing this without erasing the Sales_data object and then replacing it?

My CODE:

#include <iostream>
#include <vector>

/*
struct: Sales_data
attributes: total revenue, number of copies sold, average sales price for a book

*/

struct Sales_data
{
    //Exercise 7.2
    std::string isbn() const{return this->bookNo;}
    Sales_data& combine(const Sales_data &rhs)
    {
        this->units_sold += rhs.units_sold;
        this->revenue += rhs.revenue*rhs.units_sold;
        return *this;
    }

    Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
    {
        Sales_data sum =lhs;
        sum.combine(rhs);
        return sum;
    }

    std::string bookNo;
    unsigned units_sold =0;
    double revenue =0.0;
};

int main()
{
    Sales_data book;
    std::vector<Sales_data> vec;
    bool isDuplicate = false;

        while(std::cin>>book.bookNo>>book.units_sold>>book.revenue)
        {
            for(auto it =vec.begin(); !vec.empty()&&it!=vec.end(); ++it)
            {
                if(book.bookNo == it->isbn()) //can also be written to dereference the it pointer (*it).bookNo
                {
                    Sales_data add_book =it->add(*it, book);

                    vec.erase(it); //must erase to prevent a crash
                    vec.push_back(add_book);//push add_book obj in vec position
                    isDuplicate = true;
                }
            }

          if(!isDuplicate)
            {
                vec.push_back(book);
            }

            for(size_t i=0; i!=vec.size(); ++i)
            {
                std::cout<<vec[i].isbn()<<"  "<<vec[i].units_sold<<"  "<<vec[i].revenue<<std::endl;
            }

          isDuplicate = false;
        }

    return 0;
}

//Thank you.

Aucun commentaire:

Enregistrer un commentaire