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

shopping.cc

#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;
    }
}

main.cc

#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.

If it is core-dumping you still need to potty-train it.

Google "linked lists" and read more to learn how to fix your problems. (They are all caused by an improperly-coded linked list.)

Also, your setHead() method is very dangerous: you should not have methods that let things outside your class mess around with its private data.

Hope this helps.

Comments
Great imagery!!

I have a feeling it has to do with this line-> (shopping.h)
Shopping_Cart(string item, double price, Shopping_Cart* link);

I say that because Shopping_Cart* link is not color coated like the rest of my variables. I can't figure out whats wrong with that though. And I also tried running GDB to find out what wrong, but it is way more advanced than me, so it didn't help at all. Any ideas why and/or if this is the line thats screwing things up?

Google does not help me, because all I can find is how to do this with a struct, not a class.

Google does not help me, because all I can find is how to do this with a struct, not a class.

The difference between a class and a struct is that, with a class everything is private by default, whereas with a struct everything is public by default. So you may want to take another look into the matches you've found by Google'ing.

Thats the problem, my assignment is to write a shopping cart class with only 1 private variable, and that is the head node. So I can not have a private item, price, and link. That's what is making me have so much trouble with this.

line 68 shopping.cc: using an uninitialized variable.

Would you post the test code you have written to test that class? e.g. the *.cc file that contains main()

My new code... Same problems...

shopping.h (I have a feeling this problem is my understanding of pointers and address of's)

#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 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

shopping.cc

#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)
{
  cout << "Adding an item to the cart..." << endl;
  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)
{
  CartFramePtr discard = head;
  CartFramePtr before;
  int count=0;
  
  if (discard == NULL)
    {    
      
    }
  else
    {
      while (discard->item != item && discard->link != NULL)
	{	
	  discard = discard->link;
	  count += 1;
	}
      
      if (discard->item == item)
	{
	  for (int i = 0; i < (count - 1); i++)
	    {	    
	      before = before->link;
	    }
	  before->link = discard->link;
	  delete discard;
	}
      else
	{
	  
	}
    }
}

//*************************************************************************************
// Function: TotalPrice
// Purpose:  Calculates the total price of items in the shopping cart
//*************************************************************************************
double Shopping_Cart::TotalPrice(CartFramePtr& 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(CartFramePtr& head)
{
  cout << "Displaying contents of cart..." << endl;
  CartFramePtr iter;
  for (iter = head; iter != NULL; iter = iter->link)
    {
      cout << "Item: " << iter->item << ". Price: " << iter->price << endl;
    }
}

main.cc

#include "shopping.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
  Shopping_Cart cart;
  CartFramePtr head;
  head->item = "";
  head->price = 0.0;
  head->link = NULL;
  string item;
  double price;
  item = "baseball";
  price = 5.99;

  cart.AddItem(head, item, price);

  cart.Print(head);
  //  cart.TotalPrice(head);
  
return (0);
}

It is core dumping, after displaying the print function now! I am going insane.

Ancient Dragon, I saw what you said about the uninitialized pointer, that seemed to help, but it is still dumping, even though everything is showing up correctly, it still says core dump at the end, and if I try to add more than 1 thing into the cart.

>> head->item = "";
Head is still uninitialized -- it points to some random location in memory. One way to fix it is like this:

CartFramePtr head = new CartFrame;

I can not believe I forgot that line, thank you so much Ancient Dragon. I have written like 3 linked list programs, and you always have to do that, I can not believe I forgot that. Your the best, its all working fine now. I can send you the finished code later if you want to see it.

One More thing? I think I am really close to getting this correct, I am suppose to search for the item in the linked list, and remove it from the linked list. This is the problem child here...

void Shopping_Cart::RemoveItem(CartFramePtr& head, string item)
{
  cout << "Removing item..." << endl;
  CartFramePtr discard;
  discard = new Shopping_Cart;
  discard = head;
  CartFramePtr before;
  before = new Shopping_Cart;

  int count=0;
  
  if (discard == NULL)
    {
      cout << "No items to be removed!" << endl;
    }
  else
    {    
      while (discard->item != item && discard->link != NULL)
	{	
	  discard = discard->link;
	  count += 1;
	}
      
      if (discard->item == item)
	{
	  for (int i = 0; i < (count - 1); i++)
	    {	    
	      before = before->link;
	    }
	  before->link = discard->link;
	  delete discard;
	}
      else
	{
	  cout << "That item is not in the cart!" << endl;
	}
    }
}

line 5 is a memory leek because the very next line you toss that memory away. You can combine lines 4, 5 and 6 CartFramePtr discard = head; what I would do is save the previous value of discard before advancing to the next pointer so that you don't have to in line 24-30.

line 8 is also a memory leek

before = discard;
while (discard->item != item && discard->link != NULL)
{
     before = discard;
     discard = discard->link;
}
// delete discard
if( discard->item == item)
{
    before->link = discard->link;
    delete discard;
}

I do not understand how that would set before to the node before the node to be discarded.

This is one little problem I see in the code I posted. If the node to be discarded is the first node in the linked list then head->link has to be set to discard->link.

before = NULL;
while (discard->item != item && discard->link != NULL)
{
     before = discard;
     discard = discard->link;
}
// delete discard
if( discard->item == item)
{
    if( before == NULL)
         head->link = discard->link;
    else
        before->link = discard->link;
    delete discard;
}

What the above is doing is removing the discard node from the linked list by setting the link pointer of the previous node to the link pointer of the discard pointer. And before is the previous pointer because the code keeps track of the current pointer before incrementing to the next pointer in the list (line 4).

Never Mind, its working (with limitations). Thanks again!

This article has been dead for over six months. Start a new discussion instead.