Creating a to do list

I am creating a to do list project that will add items to a list, start listing things off one by one, printing out to do's based off of similar importance, and lastly to print the whole list. I have three files. 19ToDo.h, 19ToDo.cpp, and main.cpp. My four functions are addToList, getNextItem,
getByPriority, and printToDo. Everything seems to run smoothly except for the getByPriority always crashes the program? but I don't get an explanation as to why. I am just learning about header files and structures so i feel I didn't call the functions exactly how I should in main.


19ToDo.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
  #ifndef TODO
#define TODO


using std::string;
using std::int16_t;

static int positionInList = 0;
static int listSize = 0;

struct MyToDo
{
	string description;
	int priority;
	string dueDate;



};




#endif 

ToDo.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
#include <iostream>
#include <string>
#include "19ToDo.h"
using namespace std;
struct MyToDo ToDoList[100];



bool addToList(string description, int priority, string dueDate)
{


	if (listSize < 101)
	{
		MyToDo y = { description, priority, dueDate };
		ToDoList[listSize] = y;
		listSize++;
		return true;
	}
	else
	{
		return false;
	}

}
bool getNextItem(MyToDo &mytodo)
{
	if (listSize == 0)
	{

		return false;
	}
	else if (listSize < positionInList)
	{
		positionInList = 0;
		mytodo = ToDoList[positionInList];
		positionInList++;
	}
	else
	{
		mytodo = ToDoList[positionInList];
		positionInList++;

	}
}
bool getByPriority(MyToDo matches[100], int search)
{
	int count = 0;
	
	for (int i = 0; i < listSize; i++)
	{
		if (ToDoList[i].priority == search)
		{
			matches[count] = ToDoList[i];
			count++;
		}
	}


		return true;
	
}
void printToDo()
{

	for (int i = 0; i < listSize; i++)
	{

		cout << ToDoList[i].description << endl;
		cout << ToDoList[i].priority << endl;
		cout << ToDoList[i].dueDate << endl;
		cout << "" << endl;
	}
}

main.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
#include <iostream>
#include <string>
#include "19ToDo.h"



using namespace std;

bool addToList(string description, int priority, string dueDate);
bool getNextItem(MyToDo &mytodo);
bool getByPriority(MyToDo matches[100], int search);
void printToDo();

int main()
{
	int choice = 0;
	char nextmove = 'y';


	while (nextmove = 'y')
	{
		cout << "This will keep of the things you need to do!" << endl;
		cout << "What would you like to do?" << endl;
		cout << "1. Add to my to do list" << endl;
		cout << "2. Display the next item in my list" << endl;
		cout << "3. Search for activites of a certain priority" << endl;
		cout << "4. Print out my whole list" << endl;

		cin >> choice;

		if (choice == 1)
		{
			string description;
			int priority;
			string dueDate;


			cout << "what is the description of the activity?" << endl;
			cin.ignore();
			getline(cin, description);
			cout << "how urgent is this? rank 1-5. 1 for not really important, 5 for very important" << endl;
			cin >> priority;
			cout << "when is the due date? is day/month format" << endl;
			cin >> dueDate;

			addToList(description, priority, dueDate);
		}
		else if (choice == 2)
		{

			MyToDo y;

			getNextItem(y);

		
			cout <<"Description: " << y.description << endl;
			cout << "Priority :" << y.priority << endl;
			cout << "Due Date :" << y.dueDate << endl;
			

		}
		else if (choice == 3)
		{
			struct MyToDo matches[100];
			int search = 0;

			cout << "Enter a priority level to search for 1-5" << endl;
			cin >> search;

			getByPriority(&matches[100], search);
			for (int i = 0; i < 10; i++)
			{
				cout << "Description: " << matches[i].description << endl;
				cout << "Priority : " << matches[i].priority << endl;
				cout << "Due Date : " << matches[i].dueDate << endl;
			}
		}
		else if (choice == 4)
		{

			printToDo();
		}
		else
		{
			cout << "That was an incorrect choice!" << endl;
		}
		cout << "would you like to keep playing? y for YES" << endl;
		cin >> nextmove;

	}

	cout << "Good job keeping yourself on schedule!" << endl;
	return 0;
}
Last edited on
I haven't run your program, just looked over the code and found some errors:

1) In function getNextItem() is missed a return statement at end of body, or you should omit them and make your return type 'void'.

2) In main.cpp, line 20: Here you have an error which is very nasty, hard to detect. Maybe you don't get even a warning at your compiler. It's good style writing the literal first( ('y' == nextmove) instead of (nextmove == 'y').
Also I have seen that your programming style is C like.
Look at my code. That handles the same stuff like yours. But i have it written by using the facilities of C++.
Now it need't deal with range-errors of array access or its fixed sizes.

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
#include <string>
#include <iostream>
#include <vector>

class MyToDo
{
private:
    std::string m_description;
    int m_priority;
    std::string m_dueDate;

public:    
    // constructor
    MyToDo( std::string description
            , int priority
            , int dueDate
    ) : m_description{ description}
       ,m_priority{ priority}
       ,m_dueDate{ dueDate}
    {}
    
    // returns the class members:
    std::string description() const
    { return m_description; }
    
    int priority() const
    { return m_priority; }
    
    std::string dueDate() const
    { return m_dueDate; }
};


void addToList( MyToDo myToDo )
{ toDoList.push_back( myToDo ); }

std::vector<MyToDo> getByPriority(
    std::vector<MyToDo> & toDoList
   ,int search
)
{
    std::vector<MyToDo> matches;
    for (myToDo : toDoList)
    {
        if (search = myToDo.priority() )
        {
            matches.push_back( myToDo );
        }
    }
}

// use it such: std::cout << myToDo;
// overloads the '<<'

std::ostream& operator<< (
     std::ostream & stream
   , const MyToDo & myToDo
)
{
     stream
         << myToDo.description() << '\n'
         << myToDo.priority() << '\n'
         << myToDo.dueDate << '\n'
         << std::endl;
         
     return stream;
}

// instead of printToDo()
// also overloads the '<<'
// use it such: std::cout << toDoList;

std::ostream& operator<< (
    std::ostream & stream
  , const std::vector<MyToDo> & toDoList
)
{
    for ( myToDo : toDoList )
    {
        stream << myToDo;
    }
}


/*
 *  Making your self-growing MyToDo container:
 *      std::vector<MyToDo> toDoList;
 *    This list will be empty.
 *
 *  Adding an item at containers end:
 *       toDoList.push_back( MyToDo item );
 *
 *  Access an item in the container:
 *       toDoList.at(i);  // This checks range errors
 *     or
 *       toDoList[i];
 *
 *  Getting the size of your container:
 *       toDoList.size();
 *
 */


You could try to implement your main() by using these facilities.
NudeRob I would love to use vectors but our teacher is requiring arrays :/ Thanks for the errors you noticed in code though!! After further debugging it seems like my priority isnt being copied to my matches[100] array but I can seem to figure out why
Topic archived. No new replies allowed.