Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with my C++ program: When I run my program I get the following errors: 1. I choose the option 2. Display next task, I

Help with my C++ program:

When I run my program I get the following errors:

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".

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.

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

Main.cpp

#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) { td.getNextItem(getToDoData); 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 = 10; 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

a neglect of quality in relationship to international competitors;

Answered: 1 week ago