C++ Add the following functionality in the code given below: 1) Support ToDo Lis
ID: 3721098 • Letter: C
Question
C++ Add the following functionality in the code given below:
1) Support ToDo List items that take a space (i.e. "Got to Store")
2) Save any existing list items to a file using the class destructor (i.e. ~ToDo() )
3) If saved file exists, use constructor to open file pre-populate todo list.
4) Change `Done` function so a user can select which item to finish.
# ToDo.cpp
}
# ToDo.h
};
# main.cpp
#include "ToDo.h" using namespace std; ToDo::ToDo(){ ToDo(5); } ToDo::ToDo(int len){ length = len; list = new string[length]; } ToDo::~ToDo(){ delete [] list; } // Add an item to list void ToDo::add(string item){ if(next < length){ list[next] = item; next++; } } // Finish the last thing in list void ToDo::done(){ next--; list[next] = ""; } // Print list void ToDo::print(){ for(int i=0;i<next;i++){ cout << " " << i << " " << list[i] << endl; }}
# ToDo.h
#include <iostream> #include <string> using namespace std; // g++ main.cpp ToDo.cpp class ToDo { private: string *list; int length = 0; int next = 0; public: // Default Constructor ToDo(); // Create list of length len ToDo(int len); // Destroy out list ~ToDo(); // Add an item to list void add(string item); // Finish the last thing in list void done(); // Print list void print();};
# main.cpp
#include <iostream> #include <string> #include "ToDo.h" using namespace std; int main(){ char next = 'y'; int len = 0; string action; cout << "How long to you want your list: "; cin >> len; ToDo list(len); // Array of ToDo objects // Make sure you have a () constructor ToDo manylists[10]; // manylists[2].print(); // manylists[1].print(); while(next != 'x'){ cout << "Add to list (a)" << endl; cout << "Done list item (d)" << endl; cout << "Print list (p)" << endl; cout << "Exit list app (x)" << endl; cout << "What do you want to do: "; cin >> next; switch(next){ case 'a': cout << "Name a todo item: "; cin >> action; list.add(action); break; case 'd': list.done(); break; case 'p': list.print(); break; case 'x': // All done with todo list break; } } }Explanation / Answer
# ToDo.cpp
#include "ToDo.h"
using namespace std;
ToDo::ToDo(){
ifstream fin;
fin.open("list.txt");
int count = 0;
char str[80];
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
ToDo(count);
fin.seekp(pos, ios::beg);
while(!fin.eof())
{
fin.getline(str,80);
add(str);
}
fin.close();
}
ToDo::ToDo(int len){
length = len;
list = new string[length];
}
ToDo::~ToDo(){
if(next > 0)
{
ofstream fout;
fout.open("list.txt");
int i = 0;
while(i<next)
{
fout.write((char *)&list[i], sizeof(list[i]));
i++;
}
fout.close();
}
delete [] list;
}
// Add an item to list
void ToDo::add(string item){
if(next < length){
list[next] = item;
next++;
}
}
/ Finish the last thing in list
/*void ToDo::done(){
next--;
list[next] = "";
}*/
void ToDo::done(){
int i, input;
i = 0;
while(i<next)
{
cout<<i<<" . "<<list[i]<<endl;
i++;
}
cout<<"Input your choice";
cin>>input
i=0;
while(i<input)
i++;
while(i<next)
{
list[i] = list[i+1];
i++;
}
next--;
list[next] = "";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.