pointer not pointing

hi
I have this projct for school and its kind of a long code so I'll just put the code here to what i think its pertinent.
So i create an instance of todoui and call the function run:
driver.cpp
1
2
3
TodoUI* ui = new TodoUI;
	if (argc == 1)
		ui->run();


from that code lets see the todoui constructor:
todoui.cpp
1
2
3
4
TodoUI::TodoUI()
{
    TodoList* list = new TodoList;
}


it creates an instance of todolist, heres the constructor for that:
1
2
3
4
5
6
7
TodoList::TodoList()
:capacity(25), size(0)
{
    items = new TodoItem*[capacity];
    for (int i = 0; i < capacity; i++)
        items[i] = NULL;
}


and heres the constructor for todoitem just in case:
todoitem.cpp
1
2
3
4
TodoItem::TodoItem(string newText, int newPriority, bool newCompleted)
:text(newText), priority(newPriority), completed(newCompleted)
{
}


ok so now that that is setup lets see where list is pointing to:
todoui.cpp
cout << list;

the output for that is 0 which means its not pointing anywhere...
shouldnt it be pointing to a todolist object?
please help... i have no idea what is wrong. it was working and then i added something and it stopped working but i dont remember exactly what it was :(
Last edited on
HI ,
Could not get information with this much code . you need to past complete code to get the idea
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
#include "todoui.h"

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

TodoUI::TodoUI(string filename)
{
    TodoList* list;
    FooFile fileio;
    int count;

    TodoItem** temp = fileio.load(filename, count);
    list = new TodoList[count];
    for (int i = 0; i < count; i++)
        list->addItem(temp[i]);

    for (int i = 0; i < count; i++)
        delete temp[i];
    delete [] temp;
}

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

void TodoUI::run()
{
    int option;
    do
    {
        showMenu();
        option = input.readInt(0,9);
        switch (option)
        {
            case 1:
                addItem();
                break;
            case 6:
                cout << endl << list;
                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);
	cout << list->getSize() << endl << list->getCapacity();
	if (toupper(comp) == 'Y')
	{
		list->addItem(new TodoItem(text, priority, true));
	}
	else
	{
		list->addItem(new TodoItem(text, priority, false));
	}
}

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;
}


todolislt.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
#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::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)
		cout << "List is empty." << endl;
	else
		for (int i = 0; i < src.size; i++)
			outs << i << ": " << *src.items[i] << endl;

	return outs;
}


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
#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 += '@';
	temp += priority;
	temp += '@';
	temp += completed;
	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;
}


the CinReader and FooFile were both made and tested by my teacher so i know they are not the problem. i have the correct headers for those files too
It would be nice to see the header files, but I assume that class TodoUI has a member variable "list". In the constructor of TodoUI you declare a local variable

TodoList* list = new TodoList;,

which hides the class member variable. Therefore, the member variable is not initialized. You should write

TodoList* list = new TodoList;
Last edited on
Made some comments in the first tow files ..
Modify the files accordingly .

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

TodoUI::TodoUI()
{
    TodoList* list = new TodoList;						//Using local TodoList  wrong way
}

TodoUI::TodoUI(string filename)
{
    TodoList* list;															//Using local TodoList wrong way 
	    FooFile fileio;														//What is this FooFile 
    int count;

    TodoItem** temp = fileio.load(filename, count);							//Not able to understand why it is returing address of TodoItem
    list = new TodoList[count];																			//Wrong way
    for (int i = 0; i < count; i++)
        list->addItem(temp[i]);

    for (int i = 0; i < count; i++)                       
        delete temp[i];                                                                            //no need o f this delete as you have not allocate memory to temp
    delete [] temp;
}

TodoUI::~TodoUI()
{
    delete list;																									//Should be array of the list . 
}

void TodoUI::run()
{
    int option;
    do
    {
        showMenu();
        option = input.readInt(0,9);
        switch (option)
        {
            case 1:
                addItem();
                break;
            case 6:
                cout << endl << list;
                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);
	cout << list->getSize() << endl << list->getCapacity();
	if (toupper(comp) == 'Y')
	{
		list->addItem(new TodoItem(text, priority, true));
	}
	else
	{
		list->addItem(new TodoItem(text, priority, false));
	}
}

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;
}
YAY it was that krakatau :) thank you

another question, here are the instructions on the foofile:
1
2
3
4
5
6
7
8
9
10
11
/**
		 * 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);


how would I use that? the way i have it is not working :(

and here are the headers:
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
#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;
        TodoList* list;

        void addItem();
};


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
#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 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.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 <iostream>
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();
};
Did you fix the second similar mistake in todoui.cpp line 10 (also pointed out by bluecoder)?
If not - remove the declaration and see if it works better.
Topic archived. No new replies allowed.