Question: Help with my c++ program, please. 1. I choose the option 2. Display next task, I get an error if there is no task or

Help with my c++ program, please.

1. I choose the option "2. Display next task", I get an error if there is no task or I select this option multiple times in a row priority becomes something like "-842150451". I have narrowed it down to the "listcapacity = 5;" in ToDo.cpp I somehow need it to grow every time a new task is added so that I don't have empty tasks that give errors and have to be cycled through to get to task one. But I may be way off this is what the teacher is asking for:

  1. If the ToDoList is empty getNextItem simply returns false
  2. If the ToDoList is not empty the next itme in the ToDo list is copied to the struct passed by reference and true is returned
  3. *If the last item in the todo list is returned back, the next call to getNextItem will get the first item in the todo list again.

2. I choose the option "3.Search by priority", it will only return the first instance when I would like to see all with that set priority. I was instructed too:

We have to pass a 3rd parameter to the function td.getByPriority(tempTodo, fPrior,size).The size will return the size of the structure array.Changes are marked in Bold.

bool ToDoList::getByPriority(struct MyToDo* todoList, int priority,int& size) { int cc = 0; bool matched = false; for (int aa = 0; aa < nelem; aa++) { if (list[aa].todoPriority == priority) { todoList[cc] = list[aa]; cc++; matched = true; } } if (matched == false) { return false; } size = cc; return true; }

//in .H file

bool getByPriority(struct MyToDo* todoList, int priority,int& size);

....but was not told what to place in main.cpp and I recieve errors in the code. ( i tried td.getByPriority(tempTodo, fPrior,size))

3. After those have been fixed how would I compile ToDo.h without a class present?

Main.cpp

/* Nathan Waggoner ID# 0564342 March 1, 2020 Assignment 19 - Structure & Arrays Assignment Description: Make a ToDoList. */

#include #include #include #include"ToDo.h"

using namespace std;

int main() { int choice = 0; char nextmove = 'y'; struct MyToDo* tempTodo, getToDoItem, getToDoData; ToDoList td; int nn = 1; string todoDesc1; string date; int todoPriority1; int ss;

while (nextmove == 'y') { cout << " What would you like to do?" << endl; cout << "1. Add task" << endl; cout << "2. Display next task" << endl; cout << "3. Search by priority" << endl; cout << "4. Print list" << endl; cout << "5. Exit" << endl;

cin >> choice;

if (choice == 1) { cout << " Enter the # of task you want to add." << endl; cin >> nn; for (int aa = 0; aa < nn; aa++) { cout << " Enter Description: "; cin >> todoDesc1; cout << "Enter Priority: "; cin >> todoPriority1; cout << "Enter Due Date(mm/dd): "; cin >> date; td.addToList(todoDesc1, date, todoPriority1); } } else if (choice == 2) { if (!td.getNextItem(getToDoData)) continue; cout << " The first task on the list is:" << endl; cout << "Description: " << getToDoData.todoDesc << endl; cout << "Priority: " << getToDoData.todoPriority << endl; cout << "Due Date: " << getToDoData.dueDate << endl; cout << endl; cout << "The next tasks after that are: " << endl; for (int aa = 1; aa < nn; aa++) { td.getNextItem(getToDoData); cout << "Description: " << getToDoData.todoDesc << endl; cout << "Priority: " << getToDoData.todoPriority << endl; cout << "Due Date: " << getToDoData.dueDate << endl << endl; } } else if (choice == 3) { tempTodo = new struct MyToDo[nn]; cout << " Priority # to search for:" << endl; int fPrior; cin >> fPrior;

if (td.getByPriority(tempTodo, fPrior)) { ss = sizeof(*tempTodo) / sizeof(tempTodo[0]); for (int kk = 0; kk < ss; kk++) { cout << " Description: " << tempTodo[kk].todoDesc << endl; cout << "Priority: " << tempTodo[kk].todoPriority << endl; cout << "Due Date: " << tempTodo[kk].dueDate << endl; } } else { cout << " No priority " << fPrior << " tasks." << endl; } }

else if (choice == 4) { cout << endl; td.printToDo(); cout << endl; } else if (choice == 5) { nextmove = 'n'; } else { cout << "That was an incorrect choice!" << endl; } } cout << " Goodbye " << endl; return 0; }

ToDo.cpp

#include #include #include #include"ToDo.h"

using namespace std;

ToDoList::ToDoList() { listcapacity = 5; list = new MyToDo[listcapacity]; nelem = 0; nextElement = 0; }

bool ToDoList::addToList(struct MyToDo todo) { if (nelem == listcapacity) return false; else list[nelem++] = todo; return true; }

bool ToDoList::addToList(string desc, string ddate, int p) { if (nelem == listcapacity) return false; list[nelem].todoDesc = desc; list[nelem].dueDate = ddate; list[nelem].todoPriority = p; nelem++; return true; }

bool ToDoList::getNextItem(struct MyToDo& todo) { if (nelem == 0) return false; if (nextElement == listcapacity) nextElement = 0; todo = list[nextElement]; nextElement++; return true; }

bool ToDoList::getNextItem(string& Description, string& Date, int& priority) { if (nelem == 0) return false; if (nextElement == listcapacity) nextElement = 0; Description = list[nextElement].todoDesc; Date = list[nextElement].dueDate; priority = list[nextElement].todoPriority; nextElement++; return true; }

bool ToDoList::getByPriority(struct MyToDo* todoList, int priority) { int cc = 0; bool matched = false; for (int aa = 0; aa < nelem; aa++) { if (list[aa].todoPriority == priority) { todoList[cc] = list[aa]; cc++; matched = true; } } if (matched == false) { return false; } return true; }

void ToDoList::printToDo() { cout << "Description" << setw(20) << "Priority " << setw(15) << " Due_Date" << endl; for (int aa = 0; aa < nelem; aa++) { cout << left << setw(20) << list[aa].todoDesc << right << setw(7) << list[aa].todoPriority << right << setw(16) << list[aa].dueDate << endl; } }

ToDo.h

#ifndef todo_h #define todo_h #include

using namespace std;

struct MyToDo { string todoDesc; int todoPriority; string dueDate; };

class ToDoList { private: MyToDo* list; int nelem; int nextElement; int listcapacity; public: ToDoList(); bool addToList(struct MyToDo todo); bool addToList(string Description, string Date, int priority); bool getNextItem(struct MyToDo& todo); bool getNextItem(string& Description, string& Date, int& priority); bool getByPriority(struct MyToDo* todoList, int priority); void printToDo(); }; #endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!