I have a file that I am reading the type of object to create, the amount, and the price. I have verified that the file is being read correctly and the values are being place in the variables type, quantity, and price, however the program gets to the first candy constructor in Checkout.cpp and stops and exits before creating the object. The Candy and Cookie Class inherit from a DessertItem base class, and the vector "desserts" is of type DessertItem.
Checkout.h
class Checkout{
private:
std::vector<DessertItem*> dessertList;
std::string fileToOpen;
int type;
double quantity;
double price;
public:
//constructor
Checkout(std::string);
//read file
std::ifstream desserts{};
//sets
void setFileName(std::string);
//gets
std::string getFileName();
//read file and add correct object to vector
void readFile();
//recipt display method
void displayReceipt();
};
Relevant code in Checkout.cpp
while(desserts >> type >> quantity >> price){
if(type==1){
std::cout << "type 1 condition" << '\n';
std::cout << price << '\n';
//gets to here and then crashes
Candy* candy = new Candy(quantity,price);
dessertList.push_back(candy);
}
Candy.h
class Candy : public DessertItem{
private:
double weight;
double priceLB;
const std::string itemType= "Candy";
public:
//construtor
Candy(double,double);
//destructor
~Candy();
//sets
void setWeight(double);
void setPrice(double);
//gets
double getWeight();
double getPrice();
//virtual print
virtual void print();
//virtual calculate cost
double calculateCost(double,double);
};
Candy Constructor
Candy :: Candy(double weight, double priceLB):DessertItem(itemType, calculateCost(weight,priceLB)){
setWeight(weight);
setPrice(priceLB);
std::cout << "made candy" << '\n';
}
DessertItem.h
class DessertItem{
private:
std::string dessertName;
double dessertCost;
public:
//construtor
DessertItem(std::string, double);
//destructor
~DessertItem();
//sets
void setName(std::string);
void setCost(double);
//gets
std::string getName();
double getCost();
//virtual print
virtual void print();
//virtual calculate cost
virtual double calculateCost();
};
DessertItem.cpp
//constructor accepting 1 argument
DessertItem :: DessertItem(string name, double cost){
setName(name);
setCost(cost);
}
//destructor
DessertItem :: ~DessertItem(){}
//sets
void DessertItem :: setName(string name){
dessertName=name;
}
void DessertItem :: setCost(double cost){
dessertCost=cost;
}
//gets
string DessertItem:: getName(){
return dessertName;
}
double DessertItem :: getCost(){
return dessertCost;
}
//virtual print
void DessertItem :: print(){
}
//virtual calculate cost method
double DessertItem :: calculateCost(){
}