Coders Packet

Todo List in C++

By Mayank Chopra

This project basically maintains arecord of your completed and uncompleted tasks separately using file handling in C++ language. You can add, delete and modify your (todos) tasks.

Basically, it is a command-line argument task, it includes the following - 

- help -    it is used to guide the user about the no of actions that can be performed.

- add -     it is used to add a task

- del -      it is used to delete a task

- done -   it is used to ensure that the task marked as completed. 

-report  - it is used to display the no of completed and uncompleted tasks.

-ls  -        it is used to list the no of uncompleted tasks.

 

In the below code vector actually means string type vector 

To make the below code workable, you have to define a macro in the code  as - 

vector    // at line 3 mentioned in code.

// Further you can also refer to code available in the project File(todo.cpp) that is workable.

{  // main code

#include<bits/stdc++.h>
using namespace std;
// 3 line int main(int arg ,char ** arv) { if(arg == 1) { //basic guide for using todo list cout << "Usage :-" << "\n" << "$ ./todo add " << "\"todo item\"" << " # Add a new todo"<<"\n"<< "$./todo ls # Show remaining todos" << "\n" << "$ ./todo del NUMBER # Delete a todo"<<"\n" << "$ ./todo done NUMBER # Complete a todo"<<"\n" << "$./todo help # Show usage" << "\n" << "$ ./todo report # Statistics" << "\n"; return 0; } else if(*arv[1] == 'h') { //help cout << "Usage :-" << "\n" << "$ ./todo add " << "\"todo item\"" << " # Add a new todo" << "\n" << "$./todo ls # Show remaining todos"<< "\n" << "$ ./todo del NUMBER # Delete a todo" << "\n" << "$ ./todo done NUMBER # Complete a todo" << "\n" << "$./todo help # Show usage"<< "\n" << "$ ./todo report # Statistics" << "\n"; return 0; } string rr=arv[1]; time_t ttime = time(0); tm *local_time = localtime(&ttime); fstream fio; string line; fio.open("todo.txt", ios::out | ios::in |ios::app) ; if(rr=="add") { //add vector v; line=arv[2]; cout<<"Added todo:"<<" "<<line<<endl; fio <<line<<endl; fio.seekg(0, ios::beg); while(fio) { string str; getline(fio,str); v.push_back(str); } if(v.size()>1) { reverse(v.begin(),v.end()); fio.close(); fio.open("todo.txt", ios::out | ios::in | ios::trunc) ; for(int i=1;i<v.size();i++) { fio<<v[i]<<endl; } } } //if ended else if(rr=="del") { //delete string x=arv[2]; int z; string t; vector p; while(fio) { getline(fio,t); p.push_back(t); } fio.close(); z=stoi(x); if(z==0 || z>(p.size()-1) || z<0) { cout<<"todo #"<<z<<"does not exist"<<endl; return 0; } cout<<"Deleted todo #"<<z<<endl; fio.open("todo.txt", ios::out | ios::in | ios::trunc) ; for(int i=0;i<p.size()-1;i++) { if(i==(p.size()-1)-z) continue; else fio<<p[i]<<endl; } } else if(rr=="ls") { //list fio.seekg(0, ios::beg); vector l; while(fio) { getline(fio, line); l.push_back(line); } int a=l.size()-1; for(int i=0;i<l.size()-1;i++) { if(i==(l.size()-1)) cout<<"["<<a<<"]"<<l[i]; else cout<<"["<<a<<"]"<<l[i]<<endl; a--; } } else if(rr=="done") { //done int u=local_time->tm_mday; int q=local_time->tm_mon; int f=local_time->tm_year; string str ="x "; str=str+to_string(u)+"-"+to_string(q)+"-"+to_string(f)+" "; string a=arv[2]; int n; vector g; fstream ff; ff.open("done.txt", ios::in | ios::out | ios::app) ; fio.seekg(0, ios::beg); while(fio) { string rr; getline(fio,rr); g.push_back(rr); } fio.close(); n=stoi(a); if(n==0 || n>(g.size()-1) || n<0) { cout<<"todo #"<<n<<"does not exist"<<endl; return 0; } cout<<"Marked todo #"<<n<<" as done ."<<endl; fio.open("todo.txt", ios::out | ios::in | ios::trunc) ; for(int i=0;i<g.size()-1;i++) { if(i==(g.size()-1)-n){ str=str+g[i]; ff<<str<<endl; } else fio<<g[i]<<endl; } ff.close(); } else if(rr=="report") { //report vector j; vector n; string h; cout << local_time->tm_mday <<'/'<< 1 + local_time->tm_mon<<'/'<< 1900 + local_time->tm_year <<" "; fio.seekg(0, ios::beg); while(fio) { getline(fio,h); j.push_back(h); } fstream ff; ff.open("done.txt", ios::in | ios::app) ; fio.seekg(0, ios::beg); while(ff) { getline(ff,h); n.push_back(h); } cout<<"Pending : "<<j.size()-1<<" "<<"Completed : "<<n.size()-1<<endl;; } else cout << "invalid command" << "\n"; fio.close(); return 0; }

 

 

 

} // code ends here.

// below is a preview of a program performing different commands mentioned above.

preview

// in above screenshot firstly the file todo.cpp is compiled by MinGW compiler

// then the commands are executed for example - ./a.out add "watering the plants"

// ./ this icon represents that the file is executable 

// a.out represents the file name that is the output 

// add and the string in quotes is an argument that is stored in (**arv) character type pointer to pointer variable .

 

// below is the preview of the uncompleted task file

todos

// In the above file two tasks were added i.e -watering plants and going to market but the second is deleted and shifted to the file which

// has the list of completed tasks with mentioned time which can be seen below.

// above you can see that the second task is shifted to a new file referring to the list of completed tasks.

 

// for details commits and projects description you can refer to the GitHub repo - 

// https://github.com/nseadlc-2020/package-todo-cli-task

// Also you can refer the full code in the Project File.

 

 

Download project

Reviews Report

Submitted by Mayank Chopra (mayankchopra29)

Download packets of source code on Coders Packet