Class: member function print() not printing data..

.h file

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
#ifndef __lab4b__taskList__
#define __lab4b__taskList__
#define MAX_CHAR 100

#include <iostream>
using namespace std; 
// Define a structure to use as the list item
struct taskEntry_S
{
    int             priority;                                           // Task priority and key
    char            theTask[128];                                       // Task string;
    char            theCourse[128];                                     // Task string;
    char            theDate[128];                                       // Task string;

};

#define MAX_ENTRY    50                                                 // Define maximum number of tasks in list

class taskList {
private: 

    taskEntry_S     taskEntry[MAX_ENTRY];                               // List implemented as an array
    int             lastIndex;                                          // Index where last item was inserted

public:

    taskList();                                                         // Class constructor
    ~taskList();                                                        // Class destructor
    void ClearList();                                                   // Remove all items from the list
    void Insert(char *course, char *task, char *date, int priority);    // Add a task to the list
    void Delete(char priority);                                            // Delete given a task string
    void Delete(int priority);                                          // Delete given a priority
    char *Search(int priority);                                         // Search given a priority
    int  ListLength();                                                  // Return number of items in list
    bool isEmpty();                                                     // Return true if list is empty
    bool isFull();                                                      // Return true if list is full
    char *GetNextTask();                                                // Return the next task
    void PrintList();                                                   // Print all items in the list
};


#endif /* defined(__lab4b__taskList__) */



implementation file

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
#include "taskList.h"
#include <cstdlib>    // To get access to malloc()
#include <string.h>



//--------------------------------------------
// Function: taskList()
// Purpose: default constructor: Sets last index -1
//          so the first entry will be index key 0; 
// Returns: N/A
//--------------------------------------------
taskList::taskList()
{
    lastIndex = -1;
}

//--------------------------------------------
// Function: taskList()
// Purpose: Destuctor 
// Returns: N/A
//--------------------------------------------

taskList::~taskList()
{

}

//--------------------------------------------
// Function: ClearList()
// Purpose: Removes all items from the list. 
// Returns: N/A
//--------------------------------------------

void taskList::ClearList()
{
    lastIndex = -1; // Reseting count. 
}

//--------------------------------------------
// Function: Insert()
// Purpose: insert task into the list correct order. 
// Returns: N/A
//--------------------------------------------

void taskList::Insert(char *course, char *task, char *date, int priority)
{
    int index = 0;
    if (isFull())
    {
        cout << "Cannot add the task: " << task;
        cout << "\n to the list.  List full.\n";
        return;
    }

    // Increment lastIndex
    lastIndex++;

    // Move all tems down list until correct location is found.
    while ((index > 0) && taskEntry[index-1].priority > priority) {
        taskEntry[index] = taskEntry[index -1];
        index--; 
    }

    taskEntry[index].priority = priority;
    strcpy(taskEntry[index].theCourse, course);
    strcpy(taskEntry[index].theTask, task);
    strcpy(taskEntry[index].theDate, date);


}

//--------------------------------------------
// Function: Delete
// Purpose: deletes a task from the list
// Returns: N/A
//--------------------------------------------

void taskList::Delete(int priority)

{
    int i, k = 0;
    while ((k < lastIndex) && (taskEntry[k].priority != priority))
    {
        k++;
    }
    if (k > lastIndex) {
        cout << "Unable to find task to delete!? Please try again!!" << endl; 
    }
    else
        for (i = k ;i < lastIndex; i++)
        {
            taskEntry[i] = taskEntry[i+1];
        }
    lastIndex--;
    

}


// _____CHANGE TO COURSE_________
//--------------------------------------------
// Function: Search()
// Purpose: searchs for task by priortiy. 
// Returns: Char pointer pointing to a new
//    string containing a copy of the task.
//--------------------------------------------

char *taskList::Search(int priority)
{
    int s = 0;
    char *returnStr;
    
    while ((s <= lastIndex) && (taskEntry[s].priority))
    {
        s++;
    }
    if (s > lastIndex) {
        cout << "Task not found.\n\n";
        return ""; // could not find task
    }
    else
    {
        returnStr = (char *)malloc(strlen(taskEntry[s].theTask)+1);
        strcpy(returnStr, taskEntry[s].theTask);
        return returnStr;
    }
}

//--------------------------------------------
// Function: ListLength()
// Purpose: Returns number of items in list
// Returns: Number of items in list
//--------------------------------------------

int taskList::ListLength()
{
    return lastIndex+1;
}

//--------------------------------------------
// Function: isEmpty()
// Purpose: Returns true if list is empty.
// Returns: True if empty otherwise returns false.
//--------------------------------------------

bool taskList::isEmpty()
{
    return (lastIndex == -1); 
}

//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the list is full
// Returns: True if full, otherwise false
//--------------------------------------------

bool taskList::isFull()
{
    return (lastIndex >= MAX_ENTRY); 
}

//--------------------------------------------
// Function: GetNextTask()
// Purpose: Return the task string of the task
//    at the head of the list.
// Returns: char pointer pointing to a new
//    string containing a copy of the task.
//--------------------------------------------

char *taskList::GetNextTask()
{
    
    int i = 0;
    char *returnStr;
    if (isEmpty())
    {
        cout << "List is empty.\n\n";
        return "";
    }
    else
    {
        returnStr = (char *)malloc(strlen(taskEntry[0].theTask) + 1);
        strcpy(returnStr, taskEntry[0].theTask);
    }
        // move other tasks up
    while (i < lastIndex)
    {
        taskEntry[i] = taskEntry[i+1];
    }
    lastIndex--;
    return returnStr;

}

//--------------------------------------------
// Function: PrintList()
// Purpose: Print all tasks in the list with
//    their priority.
// Returns: void
//--------------------------------------------

void taskList::PrintList()
{
    int i;
    
    cout << "\n\nTasks in the List\n";
    cout << "-----------------------------------------------------------\n";
    cout << "Priority\t\tTask\n";
    cout << "-----------------------------------------------------------\n";
    
    for (i = 0; i < lastIndex ; i++)
    {
        cout << taskEntry[i].priority << "\t\t" << taskEntry[i].theCourse << "\t\t" << taskEntry[i].theTask << "\t\t" << taskEntry[i].theDate << "\n";
    }
}



Last edited on

main
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

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

int main(int argc, const char * argv[])
{


    taskList *theList;
    
    cout << "Simple List Demonstration\n";

    
        theList = new taskList();

    char test[] = "This is task 1";
    char test1[] = "This is task 2";
    char test2[] = "This is task 3";
    
    theList->Insert(test, test, test, 5);
    theList->Insert(test1, test1, test1, 5);
    theList->Insert(test2, test2, test2, 5);

    
    theList->PrintList();
        cout << "\nList now contains " << theList->ListLength() << " items.\n\n";
    
    return 0;
}




actual output

1
2
3
4
5
6
7
8

Simple List Demonstration


Tasks in the List
Priority		Task
-----------------------------------------------------------
5		This is task 3		This is task 3		This is task 3
0						

List now contains 3 items.





expected output

1
2
3
4
5
6
7
Simple List Demonstration


Tasks in the List
Priority		Task
-----------------------------------------------------------
5		This is task 1		This is task 1		This is task 1
5		This is task 2		This is task 2		This is task 2
5		This is task 3		This is task 3		This is task 3
List now contains 3 items.



Any help would be great.
Check your insert function, the while loop is never executed.
Thank you!!
Topic archived. No new replies allowed.