trouble with class

hi,
my teacher asked us to make a todo list project and to save the list of todo items we are supposed to use a function he made. However I can't make it work. Any help would be appreciated. the class he created is foofile. I use hos class on the todofile ui function called loadlist
here is the code
foofile.h
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
/*
Copyright (c) 2012, J Boyd Trolinger

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are  permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list 
of conditions and the following disclaimer in the documentation and/or other materials 
provided with the distribution.

Neither the name of the Butte College Department of Computer Science nor the names of its 
contributors may be used to endorse or promote products derived from this software without 
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include "TodoItem.h"
#include "TodoList.h"

#include <algorithm>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

/** \mainpage
 *  \section intro_sec Introduction
 *  Please use the <i>Classes</i> link above to access API documentation for FooFile.
 */

/**
 *   A class for handling file input and output operations.  FooFile was created 
 *   specifically to handle data from TodoItem formatted per the specification of 
 *   Programming Project 3, Spring 2012.
 *   @author J Boyd Trolinger
 *   @version 1.0
 */
class FooFile
{
	public:
	
		/**
		 * Load the contents of a file into a dynamic array of dynamic TodoItem objects.
		 * Each line of the file is assumed to be an entry for a single TodoItem, formatted
		 * in the following way -> TEXT@PRIORITY@COMPLETED. FooFile does <b>not</b> free 
		 * the dynamic memory associated with the objects or the array. The caller must free
		 * this memory.
		 * @param filename the name of the file to load
		 * @param count will be set to the number of TodoItem loaded
		 * @return a dynamic array of dynamic TodoItem objects
		 */
		static TodoItem** load (string filename, int& count);
		
		/**
		 * Save the contents of a TodoList object to file.
		 * TodoList <b>must</b> have a function <i>toFile</i> that returns string.
		 * @param filename the file to write; the file will be overwritten
		 * @param list the TodoList object on which to call <i>toFile</i>
		 * @return a bool true if the save operation succeeded, else false
		 */
		static bool save (string filename, TodoList& list);
		
	private:
	
		static TodoItem* makeItem (string line);
		static string trim (string s);
};


driver.cpp
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
#include "todoui.h"
#include "todoitem.h"
#include "todolist.h"

#include <iostream>
using namespace std;

int main (int argc, char** argv)
{
	TodoUI* ui = new TodoUI;
	if (argc == 1)
		ui->run();
	else
	{
		string param = argv[1];
		for (int i = 0; i < param.length(); i++)
			param[i] = tolower(param[i]);
		if (param == "help" || param == "?")
			ui->help();
		else
		{
		    TodoUI* ui = new TodoUI(param);
			ui->run();
		}
	}
}


todoui.cpp
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
#include "todoui.h"

TodoUI::TodoUI()
{
    list = new TodoList;
}

TodoUI::TodoUI(string filename)
{
    list = new TodoList;

    loadList(filename);
}

TodoUI::~TodoUI()
{
    delete list;
}

void TodoUI::run()
{
    int option;
    do
    {
        showMenu();
        option = input.readInt(0,9);
        switch (option)
        {
            case 1:
                addItem();
                break;
            case 2:
                editItem();
                break;
            case 3:
                deleteItem();
                break;
            case 4:
                list->sortPriority();
                break;
            case 5:
                list->sortComplete();
                break;
            case 6:
                cout << endl << *list;
                break;
            case 7:
                cout << "This will delete all your entries." << endl;
                cout << "Are you sure you want to continue (y/n)? ";
                if (toupper(input.readChar("YyNn")) == 'Y')
                    deleteAll();
                break;
            case 8:
                saving();
                break;
            case 9:
                cout << "List name(include extension): ";
                loadList(input.readString(false));
                break;
            case 0:
                break;
        }
    }while (option != 0);
}

void TodoUI::showMenu()
{
	cout << endl << "\t\tPERSONAL TODO LIST" << endl << endl;
	cout << "[1] Create new item" << endl;
	cout << "[2] Edit an item" << endl;
	cout << "[3] Delete an item" << endl;
	cout << "[4] Sort by priority" << endl;
	cout << "[5] Sort by completion" << endl;
	cout << "[6] View all items" << endl;
	cout << "[7] Delete all items" << endl;
	cout << "[8] Save list" << endl;
	cout << "[9] Retrieve list" << endl;
	cout << "[0] Quit" << endl;
	cout << endl << "> ";
}

void TodoUI::addItem()
{
	cout << endl;
	cout << "Priority (1-5): ";
	int priority = input.readInt(1, 5);
	cout << "Completed (y/n): ";
	char comp = input.readChar("YyNn");
	cout << "Text: ";
	string text = input.readString(false);

	if (toupper(comp) == 'Y')
	{
		list->addItem(new TodoItem(text, priority, true));
	}
	else
	{
		list->addItem(new TodoItem(text, priority, false));
	}
}

void TodoUI::editItem()
{
    cout << endl;
    cout << "Index of item to edit: ";
    int ind = input.readInt(0,list->getSize());
    TodoItem* temp = list->retrieve(ind);

    cout << *temp << endl;
    cout << "Edit priority (y/n)? ";
    if (toupper(input.readChar("YyNn")) == 'Y')
    {
        cout << "Priority (1-5): ";
        temp->setPriority(input.readInt(1, 5));
    }

    cout << "Change completion status (y/n)? ";
    if (toupper(input.readChar("YyNn")) == 'Y')
    {
        if (!temp->getCompleted())
        {
            temp->setCompleted(true);
            cout << "Completed: YES" << endl;
        }
        else
        {
            temp->setCompleted(false);
            cout << "Completed: NO" << endl;
        }
    }

    cout << "Edit text (y/n)? ";
    if (toupper(input.readChar("YyNn")) == 'Y')
    {
        cout << "Text: ";
        temp->setText(input.readString(false));
    }


}

void TodoUI::deleteItem()
{
    cout << "Index of item to delete: ";
    int ind = input.readInt(0,(list->getSize()-1));
    list->deleteItem(ind);
}

void TodoUI::deleteAll()
{
    for (int i = 0; i < list->getSize(); i++)
        list->deleteItem(i);
}

void TodoUI::saving()
{
    cout << "List name: ";
    string file = input.readString(false);
    file += ".txt";
    fileio.save(file, *list);
}

void TodoUI::loadList(string filename)
{
    int count;
    TodoItem** temp = fileio.load(filename, count);

    cout << **temp;
    for (int i = 0; i < count; i++)
        list->addItem(*temp);

    //for (int i = 0; i < count; i++)
    //    delete temp[i];
    //delete [] temp;
}
void TodoUI::help()
{
	cout << "Makes a list of todo items." << endl;
	cout << endl;
	cout << "todolist [filename] [help] [?]" << endl;
	cout << endl;
	cout << "[filename]\tRetrieves a list from the file" << endl;
	cout << "help\t\tShows this screen" << endl;
	cout << "?\t\tShows this screen" << endl;
	cout << endl;
	cout << "Only first attribute will be taken into consideration." << endl;
	cout << "i.e. todolist help example - will show help screen" << endl;
}


todoui.h
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
#pragma once
#include "todoitem.h"
#include "todolist.h"
#include "foofile.h"
#include "CinReader.h"
#include <iostream>
using namespace std;

class TodoUI
{
	public:

		TodoUI();
		TodoUI(string filename);
		~TodoUI();

		void run();
		void help();
		void showMenu();

	private:
        CinReader input;
        FooFile fileio;
        TodoList* list;

        void addItem();
        void editItem();
        void deleteItem();
        void deleteAll();
        void saving();
        void loadList(string filename);
};


todolist.cpp
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
#include "todolist.h"

TodoList::TodoList()
:capacity(25), size(0)
{
    items = new TodoItem*[capacity];
    for (int i = 0; i < capacity; i++)
        items[i] = NULL;
}

TodoList::~TodoList()
{
    for (int i = 0; i < capacity; i++)
        if (items[i] != NULL)
            delete items[i];
    delete [] items;
}

string TodoList::toFile()
{
    string temp;
    for (int i = 0; i < size; i++)
        temp += items[i]->toFile() + '\n';
    return temp;
}

void TodoList::addItem (TodoItem* newItem)
{
	if (size >= capacity)
		grow();
	items[size++] = newItem;
}

void TodoList::deleteItem (int index)
{
	delete items[index];
	size--;
	pack(index);
}

TodoItem* TodoList::retrieve (int index)
{
	return items[index];
}

void TodoList::pack (int deleteIndex)
{
	for (int i = deleteIndex; i< size; i++)
		items[i] = items[i+1];
}

int TodoList::getSize()
{
	return size;
}

int TodoList::getCapacity()
{
	return capacity;
}

void TodoList::sortPriority ()
{
	for (int i = 0 ; i < size; i++)
		for (int j = i+1; j < size; j++)
			if (items[i]->getPriority() > items[j]->getPriority())
				swap(items[i], items[j]);
}

void TodoList::sortComplete ()
{
	for (int i = 0 ; i < size; i++)
		for (int j = i+1; j < size; j++)
			if (items[i]->getCompleted() == false && items[j]->getCompleted() == true)
				swap(items[i], items[j]);
}

void TodoList::grow()
{
	capacity += 10;
	TodoItem** newItems = new TodoItem*[capacity];
	for (int i = 0; i < size; i++)
		newItems[i] = items[i];

	delete [] items;

	items = newItems;
}

ostream& operator<< (ostream& outs, const TodoList& src)
{
	if (src.size ==0)
		outs << "List is empty." << endl;
	else
		for (int i = 0; i < src.size; i++)
			outs << i << ": " << *src.items[i] << endl;

	return outs;
}


todolist.h
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
#pragma once
#include "todoitem.h"
#include <iostream>
using namespace std;

class TodoList
{
	public:
        TodoList();
        ~TodoList();

        string toFile();
        void addItem (TodoItem* newItem);
        TodoItem* retrieve (int index);
        void sortPriority ();
        void sortComplete ();
        void deleteItem (int index);

        int getSize();
        int getCapacity();

        friend ostream& operator<< (ostream& outs, const TodoList& src);

	private:
        TodoItem** items;
        int capacity;
        int size;

        void grow();
        void pack (int deleteIndex);
};
todoitem.cpp
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
#include "todoitem.h"

TodoItem::TodoItem(string newText, int newPriority, bool newCompleted)
:text(newText), priority(newPriority), completed(newCompleted)
{
}

TodoItem::~TodoItem()
{
}

string TodoItem::getText()
{
	return text;
}

int TodoItem::getPriority()
{
	return priority;
}

bool TodoItem::getCompleted()
{
	return completed;
}

void TodoItem::setText(string newText)
{
	text = newText;
}

void TodoItem::setPriority(int newPriority)
{
	priority = newPriority;
}

void TodoItem::setCompleted(bool newCompleted)
{
	completed = newCompleted;
}

bool TodoItem::hasKeyword(string src)
{
    int found = text.find(src);
	return (found!=string::npos);
}

string TodoItem::toFile ()
{
	string temp;
	temp = text;
	temp += '@';
	char buffer [33];
	itoa(priority, buffer, 10);
	temp += buffer;
	temp += '@';
	if (completed)
        temp += '1';
    else
        temp += '0';
	temp += "/n";

	return temp;
}

void TodoItem::scrub()
{
	for (int i = 0; i<text.length(); i++)
		if (text[i] == '@')
			text[i] = '#';
}

ostream& operator<< (ostream& outs, const TodoItem& src)
{
	outs << src.text << endl;
	outs << "Priority: " << src.priority;
	outs << "\tCompleted: ";
	if (src.completed == true)
		outs << "YES" << endl;
	else
		outs << "NO" << endl;

	return outs;
}


todoitem.h
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
#pragma once
#include <iostream>
#include <cstdlib>
using namespace std;

class TodoItem
{
	public:

		TodoItem(string newText, int newPriority = 1, bool newCompleted = false);
		~TodoItem();

		string getText();
		int getPriority();
		bool getCompleted();

		void setText(string newText);
		void setPriority(int newPriority);
		void setCompleted(bool newCompleted);

		bool hasKeyword(string src);
		string toFile (); // formats data to file

		friend ostream& operator<< (ostream& outs, const TodoItem& src);

	private:
		string text;
		int priority;
		bool completed;

		void scrub();
};
Topic archived. No new replies allowed.