Linked List Copy Constructer Problem

I am trying to write the copy constructor for the following Menu class. When I run it, it doesn't show me anything. it just waits couple seconds than windows gives error and stop the application.Please help.
By the way firstNodePtr,lastNodePtr,titlePtr,numberOfOptions are already defined in header file.

This is the Copy Constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Student::Menu::Menu
(
 const Menu &original
 )  	
{
    char* titleCopy = new char[MAX_TITLE_LENGTH];
    strcpy(titleCopy,original.titlePtr);
    Menu copy(titleCopy);
    if (firstNodePtr != NULL)
    {
        lastNodePtr = firstNodePtr;
        while(lastNodePtr != NULL)
        {
            copy.addOption(lastNodePtr->optionTextPtr);
            lastNodePtr = lastNodePtr->link;
        }
    }
}


This is the whole Menu class
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>
#include <iomanip>
using namespace std;

#include "menu2.h"

extern const string MY_ID_INFO =
    "DJ_TLG";
extern const string MY_SUBMISSION_INFO =
    "Lab";
#pragma warning(disable:4996)

bool tracingIsOn;
Student::Menu::Menu()
{
    titlePtr = new char[11];
    strcpy(titlePtr,"Empty Menu");
    numberOfOptions = 0;
    firstNodePtr = 0;
    lastNodePtr = 0;
    if (tracingIsOn)
    {
        cout << "\nStart of default constructor trace.";
        cout << "\nGot storage for title: Empty Menu";
        cout << "\nCopied title: " << titlePtr;
        cout << "\nEnd of default constructor trace.";
        cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
    }
}
Student::Menu::Menu
( 	
    const string& menuTitle
)
{
    if (menuTitle.length() > MAX_TITLE_LENGTH)
    {
        cout<<"\n===== Error: Menu title cannot exceed 70 characters in length."
        "\nThe title \n<<"<<menuTitle<<">>\n"
        "was not added to the menu. \nThis menu will be given the following " 
        "default title: Empty Menu \nPress Enter to continue ...";
        cin.ignore(80,'\n');
        setTitle("Empty Menu");
        if (tracingIsOn)
        {
            cout << "\nStart of one-parameter constructor trace.";
            cout << "\nGot storage fortitle: Empty Menu";
            cout << "\nCopied title: " << titlePtr;
            cout << "\nEnd of one-parameter constructor trace.";
            cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
        }
    }
    else
    {
        titlePtr = new char[MAX_TITLE_LENGTH];
        strcpy(titlePtr,menuTitle.c_str());
        if (tracingIsOn)
        {
            cout << "\nStart of one-parameter constructor trace.";
            cout << "\nGot storage for title: " << menuTitle;
            cout << "\nCopied title: " << titlePtr;
            cout << "\nEnd of one-parameter constructor trace.";
            cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
        }
    }
    numberOfOptions = 0;
    firstNodePtr = 0;
    lastNodePtr = 0;
}

Student::Menu::~Menu()
{
    if (firstNodePtr == 0)
    {
        if (tracingIsOn && titlePtr != 0)
        {
            cout << "\nStart of destructor trace.";
            cout << "\nDeleting storage for this title: "
                 << titlePtr;
        }
        titlePtr = 0;
    }
    else
    {
        if (tracingIsOn && titlePtr != 0)
        {
            cout << "\nStart of destructor trace.";
            cout << "\nDeleting storage for this title: "
                 << titlePtr;
        }
        titlePtr = 0;
        
        while(firstNodePtr != NULL)
        {
            lastNodePtr = firstNodePtr;
            if (tracingIsOn)
                cout << "\nDeleting storage for this option: "
                     << lastNodePtr->optionTextPtr;
            firstNodePtr->optionTextPtr = NULL;
            if (tracingIsOn)
                cout << "\nDeleting Node storage for that option.";
            firstNodePtr = firstNodePtr->link;
            delete lastNodePtr;
        }
    }
    if (tracingIsOn)
    {
        cout << "\nEnd of destructor trace.";
        cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
    }
}

Student::Menu::Menu
(
 const Menu &original
 )  	
{
    char* titleCopy = new char[MAX_TITLE_LENGTH];
    strcpy(titleCopy,original.titlePtr);
    Menu copy(titleCopy);
    if (firstNodePtr != NULL)
    {
        lastNodePtr = firstNodePtr;
        while(lastNodePtr != NULL)
        {
            copy.addOption(lastNodePtr->optionTextPtr);
            lastNodePtr = lastNodePtr->link;
        }
    }
}

/*Menu& Student::Menu::operator=
(
 const Menu &rhs
 )
{
}*/

void Student::Menu::setTitle 	
( 	
    const string& menuTitle 	 
)
{
    if (menuTitle.length() > MAX_TITLE_LENGTH)
    {
        cout<<"\n===== Error: Menu title cannot exceed 70 characters in length."
        "\nThe title \n<<"<<menuTitle<<">>\n"
        "was not added to the menu. \nPrevious title remains unchanged."
        "\nPress Enter to continue ...";
        cin.ignore(80,'\n');
    }
    else
    {
        if (tracingIsOn) cout << "\nStart of setTitle() trace.";

        if (tracingIsOn && titlePtr != 0)
            cout << "\nDeleting this title: " << titlePtr;
        titlePtr = new char[MAX_TITLE_LENGTH];
        strcpy(titlePtr,menuTitle.c_str());
        if (tracingIsOn)
        {
            cout << "\nGot storage for title: " << menuTitle;
            cout << "\nCopied title: " << titlePtr;
            cout << "\nEnd of setTitle() trace.";
            cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
        }
    }
}

void Student::Menu::addOption 	
(
    const string& option
)
{
    if (tracingIsOn)cout << "\nStart of addOption() trace.";
    if(numberOfOptions >= MAX_NUM_OPTIONS)
    {
        cout<<"\n===== Error: Number of menu options cannot exceed 20.\n"
            "Option <<"<<option<<">> not added.\n"
            "Press Enter to continue ..."; cin.ignore(80,'\n');
    }
    else if (option.length() >= MAX_OPTION_LENGTH)
    {
        cout<<"\n===== Error: Menu option cannot exceed 70 characters in length."
            "\nThe option\n<<"<<option<<">>\nwas not added to the menu.\n"
            "Press Enter to continue ..."; cin.ignore(80,'\n');
    }
    else
    {
        char* cOption = new char[MAX_OPTION_LENGTH];
        strcpy(cOption,option.c_str());
        numberOfOptions++;
        if (firstNodePtr == 0)
        {
            if (tracingIsOn) cout << "\nGetting storage for first OptionNode.";
            firstNodePtr = new OptionNode;
            lastNodePtr = firstNodePtr;
            lastNodePtr->optionTextPtr = cOption;
            if (tracingIsOn)
            {
                cout << "\nGot storage for option: " << option;
                cout << "\nCopied option: " << lastNodePtr->optionTextPtr;
                cout << "\nEnd of addOption() trace.";
                cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
            }
            lastNodePtr->link=NULL;
        }
        else
        {
            if (tracingIsOn) cout << "\nGetting storage for next OptionNode.";
            lastNodePtr->link = new OptionNode;
            lastNodePtr = lastNodePtr->link;
            lastNodePtr->optionTextPtr = cOption;
            if (tracingIsOn)
            {
                cout << "\nGot storage for option: " << option;
                cout << "\nCopied option: " << lastNodePtr->optionTextPtr;
                cout << "\nEnd of addOption() trace.";
                cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
            }
            lastNodePtr->link=NULL;
        }
    }
}

void Student::Menu::display()const
{
    cout << string(25,'\n');
    if (numberOfOptions == 0)
    {
        cout<<titlePtr<<endl<<endl;
        cout << "This menu currently has no options ...";
    }
    else
    {
        cout <<string(((80-strlen(titlePtr))/2),' ')<< titlePtr<<endl<<endl;
        OptionNode* print = new OptionNode;
        print = firstNodePtr;
        OptionNode* indent = new OptionNode;
        indent = firstNodePtr;
        int lineLength = 0;
        while(indent != NULL)
        {
            if((int)strlen(indent->optionTextPtr) > lineLength)
                lineLength = strlen(indent->optionTextPtr);
            indent = indent->link;
        }
        while(print != NULL)
        {
            for(int i = 1; i <= numberOfOptions; i++)
            {
                cout<<setw((80-lineLength)/2)<<i<<". "<<print->optionTextPtr<<endl;
                print = print->link;
            }
        }
    }
    cout << string(((24-(numberOfOptions+2))/2), '\n');
}


/*int Student::Menu::getChoice
(
 int maxNumTries = 3,
 string userPrompt = "Enter the number of your menu choice here and press Enter: "
 )const
{
}*/
The entire problem essentially boils down to the fact that you're using char arrays instead of strings.
Moreover, your program is full of memory leaks. There are lots of news and new[] but not a single delete or delete[].
Replace titlePtr by a string called title and replace the linked list structure by std::list. After these fixes, there should be no new/new[]s left, let alone calls to strcpy/strlen. You should also be able to remove the copy constructor and let the default one do its work.
If it was up to me i would take that route too. Unfortunately this is the recitation of last week which i couldn't complete due to my busy schedule. He provide us with a header file in which titlePtr is already defined. All i can do is to make my code work with his file.

Here is the privates in the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private:
    static const int MAX_NUM_OPTIONS = 20;
    static const int MAX_OPTION_LENGTH = 70;
    static const int MAX_TITLE_LENGTH = 70;

    int numberOfOptions;
    //Number of options currently on the menu.

    char* titlePtr;
    struct OptionNode
    {
        char* optionTextPtr;
        OptionNode* link;
    };
    OptionNode* firstNodePtr;
    OptionNode* lastNodePtr;

Topic archived. No new replies allowed.