C++ Display Function in a Class for Outputting Private Data to a CLL

I'm having some issues getting a class to output some information, when called from another class. I can't seem to figure out what is going wrong, and was hoping someone would be able to point out what is happening, or give me a push in the right direction.

The problem takes place in the queue::display function when I try to call the a_entry.display function. It returns garbage and I'm trying to figure out what is wrong with the entry::display function to make it not output correctly.

Let me know if you are able to assist.

Thank you.

Here is the code:
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <cstring>
#include <cctype>
#include <iostream>

using namespace std;

class entry
{
  public:
    entry(void);
    ~entry(void);
    int create_entry(char*name, char*notes);
    void display(void);
    int copy_entry(const entry & copy_from);
    int retrieve(char * match, entry&found);

  private:
    char * name;
    char * notes;
};

entry::entry()
{

}

entry::~entry()
{
  //delete [] name;
  //delete [] notes;
}

struct q_Node
{
  entry a_entry;
  q_Node * next;
};

class queue
{
  public:
    queue(void);
    ~queue(void);
    int enqueue(const entry & to_add);
    int display();
    bool isEmpty();

  private:
    q_Node * rear;
    q_Node * front;
    int count;
};

queue::queue()
{
  front = rear = NULL;
  count = 0;
}

queue::~queue()
{
  rear->next = NULL;
  q_Node * temp;

  while(front)
  {
    temp=front->next;
    front->a_entry.~entry();
    delete front;
    front = temp;
  }
  rear = NULL;
}

int queue::enqueue(const entry & to_add) 
{ 
    if (front == NULL)
    { 
      cout << "Adding to front of queue." << endl;
      front = new q_Node;
      front->a_entry.copy_entry(to_add);
      rear = front;
      ++count;
      return 1;
    }

    else
    {
      cout << "Adding to the end of the queue." << endl;
      q_Node * temp = new q_Node;
      temp->a_entry.copy_entry(to_add);
      rear->next=temp;
      rear=temp;
      rear->next=front;
      ++count;
      return 1;
    }

    return 0; 
}

void entry::display()
{
  cout << "Route name: "<< name << endl;
  cout << "Route notes: " << notes << endl;
}

int queue::display() 
{ 
    q_Node *temp = front; 
    if (isEmpty())
      return 0;

    for(int i = 0; i < count; ++i)
    {
      temp->a_entry.display();
      temp = temp->next; 
    }

    return 1;
}

bool queue::isEmpty()
{
  if (count == 0)
    return 1;

  else
    return 0;
}

int entry::create_entry(char *name, char *notes)
{
  name = name;
  notes = notes;

  return 1;
}

int entry::copy_entry(const entry & copy_from)
{
  if (name)
    delete [] name;
  if (notes)
    delete [] notes;

  name = notes = NULL;

  if(copy_from.name)
  {
    name = new char[strlen(copy_from.name)+1];
    strcpy (name, copy_from.name);
  }
  else
    return 0;

  if (copy_from.notes)
  {
    notes = new char[strlen(copy_from.notes)+1];
    strcpy (notes, copy_from.notes);
  }
  else
    return 0;

  return 1;
}

int entry::retrieve(char*match, entry&found)
{
  if (!name || !match)
    return 0;

  if (!strcmp(name, match))
  {
    if(found.name)
      delete [] found.name;
    if(found.notes)
      delete [] found.notes;
    found.name=found.notes=NULL;

    found.name = new char[strlen(name)+1];
    strcpy(found.name, name);

    found.notes = new char[strlen(notes)+1];
    strcpy(found.notes, notes);

    return 1;
  }

  return 0;
}

int main()
{

  char name[100], notes[100];
  entry to_add;
  queue my_route;

  cout << "Enter route name:" << endl;
  cin.get(name, 100); cin.ignore(100,'\n');
  cout << "Enter route notes:" << endl;
  cin.get(notes, 100); cin.ignore(100,'\n');
  to_add.create_entry(name, notes);
  my_route.enqueue(to_add);

  cout << "Enter route name:" << endl;
  cin.get(name, 100); cin.ignore(100,'\n');
  cout << "Enter route notes:" << endl;
  cin.get(notes, 100); cin.ignore(100,'\n');
  to_add.create_entry(name, notes);
  my_route.enqueue(to_add);

  my_route.display();

  cout << "END" << endl;
  

  return 0;

}


Here's the input/output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Enter route name:
test
Enter route notes:
test
Adding to front of queue.
Enter route name:
test
Enter route notes:
test
Adding to the end of the queue.
Route name: �v�
Route notes: H�
Route name: �v�
Route notes: H�

Last edited on
On lines 134 and 135 above, you are doing this:

1
2
  name = name;
  notes = notes;

That assigns the variables to themselves! You want to assign them to your class members, like so:

1
2
  this->name = name;
  this->notes = notes;

Your compiler should've warned you about that. Don't ignore compiler warnings. If it didn't emit a warning, then turn up the warning level to maximum. E.g., with g++ or clang++ you add the flags -Wall -W -pedantic.
Last edited on
Topic archived. No new replies allowed.