counter trouble

Can anybody help me solve the problem in my code?

I am trying to place a counter to count in each time a new order comes in, but I am having trouble with the memory allocation part.

I allocate memory to take in names each time an order comes through, but when I print out the names, I get 0x100103ae0....



Last edited on
on line 53 and 54 you print addresses. Try First->GetName() instead
Also, increase the counter when you put an order
Couple of problems that I noticed are,

1
2
cout << First << endl;
cout << Last << endl;


You will have to overload the << operator in your Pizza class to make the above code work. Else, you will have to specifically state what it is you want to output. For example,

1
2
cout << First->GetName() << endl;
cout << Last->GetName() << endl;


Also, in your PizzaStore class, you will have to override the destructor to avoid the memory leaks that you are having right now.

EDIT: replaced the '.'s with '->'s
Last edited on
Thanks for the code, that solved my print issue.
1
2
3
        cout << First->GetName() << endl;
        cout << Last->GetName() << endl;


but I'm still confused on the memory leaks part.
1
2
3
4
5
        if(First == NULL)           //check to see if first is equal to NULL
        {
            First=temp;             //First equal temp
            Last = temp;   // make last also point to temp.
        }


At present you are creating a linked list of all the Pizza nodes. However, you also need to delete nodes from this list as and when the Pizza is served.

Also, when the PizzaStore object PS goes out of scope, the nodes that were present in this list get leaked out. You need to delete all these nodes from the list and make it empty in your destructor. Something like,

1
2
3
4
5
6
7
8
9
10
~PizzaStore () {
  while (First != Last) {
    Pizza *temp = First;
    First = First->next_pizza;
    delete temp;
  }
  Last = Null;
  delete First;
  First = Null;
}
Last edited on
Thanks, am I implementing the destructor correctly?

The pointers chapter and the classes with constructors and destructors chapter are the hardest for me and I can't seem to get a good understanding of them. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <iostream>
#include <string>
using namespace std;

// #include "PIZZA.cpp" // You supply this file

class Pizza;
class PizzaStore;
//=============================================================================================
//================Pizza Class==================================================================
class Pizza
{
    
public:
    string name, address, topping;        //declare variables name,address,topping,
    Pizza *next_pizza;
    //------------------------------------
    Pizza(string n, string a, string t)         //Pizza function
        { name = n; address = a; topping = t;}
    string GetName()                            //GetName function
        {  return name;}
    string GetAddress()                         //GetAddress function
        { return address;}
    string GetIngredients()                     //GetIngredients function
        { return topping;}
};
//=============================================================================================
//================PizzaStore Class=============================================================
class PizzaStore
{
private:
    Pizza *First, *Last;
    int counter;
    //------------------------------------
public:
    PizzaStore()            //PizzaStore ***** CONSTRUCTOR ******
    {
        counter = 0;
        First = NULL;
        Last = NULL;
    }
    int PizzasWaiting()     //PizzasWating function to return counter
        { return counter;}
    
    //=============having trouble here================================
    void Order(string n, string a, string t)  //Order function
    {
        //cout << n << " " << a << " " << t << endl;
        counter++;
        /*---Having trouble below ---------------*/
        Pizza *temp = new Pizza(n,a,t);  //allocate memory to hold name,address, topping, & *new_pizza
        temp->next_pizza=NULL;      //set what ever temp points to, set next_pizza to NULL
        if(First == NULL)           //check to see if first is equal to NULL
        {
            First=temp;             //First equal temp
        }
        else
        {
            Last->next_pizza= temp; //make the last pizza's next_pizza pointer point to where temp points
        }Last = temp;       //set last name equal to temp to stop when the last name is reached.


Last edited on
Why don't you use STL containers instead of make yourself a linked list.
I have not gotten to that chapter yet...
>Thanks, am I implementing the destructor correctly?

Yes it seems to be OK. I would add one more check to see if the list is not empty.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
~PizzaStore ()          //Destructor.......
    {
        if (First) {
          while (First != Last)
          {
              Pizza *temp = First;
              First = First->next_pizza;
              delete temp;
          }
          Last = NULL;
          delete First;
          First = NULL;
       }
    }


Also, you have not included the line that I suggested in your Order function.

To get your basics right, the tutorial on this website is a good resource for learning.
This is what I have so far. I have implemented a delivery method and updated the destructor.

Am I on the right path?
Last edited on
Debug it in bits and you'll see where the bugs are. Also, try to make your functions as safe as possible. For eg., in your Deliver() function, you should first check whether the linked list has any Pizza's waiting otherwise it may crash.

Note: You still haven't added the line in the Order function that i suggested. You may end up with problems.
1
2
3
4
5
if(First == NULL)          
        {
            First=temp;     
            Last = temp;   // make last also point to temp.
        }
Cool, should I erase "Last=temp", the one that I have at the end of the if statment in my order function?

1
2
3
4
5
6
7
8
9
        if(First == NULL)           //check to see if first is equal to NULL
        {
            First=temp;             //First equal temp
            Last = temp;
        }
        else
        {
            Last->next_pizza= temp; 
        }Last = temp;              //  <---- should I erase this.... 

Last edited on
Oops.. sorry missed that statement... dont make any change as you are already making last equal to temp.


code finally solved.....
Last edited on
Topic archived. No new replies allowed.