Hello, I have to write a shopping cart class in c++ and I am getting a core dump, and I have no idea why. I know where it is core dumping. Its dumping at 2 places-> Shopping_Cart::Print(Shopping_Cart* head) and Shopping_Cart::TotalPrice(Shopping_Cart* head)
Here are my 3 files...
shopping.h
#ifndef SHOPPING_CART_H
#define SHOPPING_CART_H 1
#include <string>
using namespace std;
class Shopping_Cart
{
public:
string item;
double price;
Shopping_Cart* link;
typedef Shopping_Cart* CartFramePtr2;
Shopping_Cart(); // Default Constructor
Shopping_Cart(string item, double price, Shopping_Cart* link); // Constructor
Shopping_Cart* getHead() const; // Accessor
void setHead(Shopping_Cart* NewHead); // Mutator
void AddItem(Shopping_Cart* head, string item, double price); // Functions to manipulate data
void RemoveItem(Shopping_Cart* head, string item, double price);
double TotalPrice(Shopping_Cart* head);
void Print(Shopping_Cart* head);
private:
Shopping_Cart* head; // The one private member we are allowed
};
typedef Shopping_Cart* CartFramePtr;
#endif
#include "shopping.h"
#include <iostream>
#include <string>
using namespace std;
//*************************************************************************************
// Constructors
//*************************************************************************************
Shopping_Cart::Shopping_Cart()
{
}
Shopping_Cart::Shopping_Cart(string item, double price, CartFramePtr link)
{
}
//*************************************************************************************
// Accessor
//*************************************************************************************
Shopping_Cart* Shopping_Cart::getHead() const
{
return(head);
}
//*************************************************************************************
// Mutator
//*************************************************************************************
void Shopping_Cart::setHead(CartFramePtr NewHead)
{
head = NewHead;
}
//*************************************************************************************
// Function: AddItem
// Purpose: Adding and item into the shopping cart
//*************************************************************************************
void Shopping_Cart::AddItem(CartFramePtr head, string item, double price)
{
CartFramePtr temp_ptr;
temp_ptr = new Shopping_Cart;
temp_ptr->item = item;
temp_ptr->price = price;
temp_ptr->link = head;
head = temp_ptr;
}
//*************************************************************************************
// Function: RemoveItem
// Purpose: Removing an item from the shopping cart
//*************************************************************************************
void Shopping_Cart::RemoveItem(CartFramePtr head, string item, double price)
{
}
//*************************************************************************************
// Function: TotalPrice
// Purpose: Calculates the total price of items in the shopping cart
//*************************************************************************************
double Shopping_Cart::TotalPrice(Shopping_Cart* head)
{
double total;
CartFramePtr iter;
for (iter = head; iter != NULL; iter = iter->link)
{
total += iter->price;
}
return(total);
}
//*************************************************************************************
// Function: Print
// Purpose: Displays (on the screen) all items in the shopping cart.
//*************************************************************************************
void Shopping_Cart::Print(Shopping_Cart* head)
{
CartFramePtr iter;
for (iter = head; iter != NULL; iter = iter->link)
{
cout << "Item: " << iter->item << ". Price: " << iter->price << endl;
}
}
#include "shopping.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Shopping_Cart cart;
CartFramePtr head;
cart.setHead(head);
string item;
double price;
item = "baseball";
price = 5.99;
cart.AddItem(head, item, price);
cart.Print(head);
cart.TotalPrice(head);
return (0);
}
Thanks.