#include <list>
#include <stdio.h>
using namespace std;
int main()
{
list<char *> toDo;
int command;
FILE *f = fopen("toDoList", "r");
if (f == NULL)
{
fclose(fopen("toDoList", "w"));
f = fopen("toDoList", "r");
}
char *toAdd;
while (!feof(f))
{
toAdd = new char[256];
fgets(toAdd, 256, f);
if (!feof(f))
toDo.push_back(toAdd);
}
fclose(f);
printf("Welcome to the To-Do List.\n1) Print the list\n");
printf("2) Add to the list\n3) Erase the list\n4) Exit\n");
while (true)
{
printf("What would you like to do? ");
scanf("%d", &command);
if (command == 1)
{
printf("Your list:\n");
for (auto i = toDo.begin(); i != toDo.end(); i++)
printf("%s", *i);
}
if (command == 2)
{
printf("What would you like add? ");
toAdd = new char[256];
fflush(stdin);
fgets(toAdd, 257, stdin);
toDo.push_back(toAdd);
}
if (command == 3)
{
for (auto i = toDo.begin(); i != toDo.end(); i++)
delete [](*i);
toDo.clear();
}
if (command == 4)
break;
}
f = fopen("ToDoList", "w");
for (auto i = toDo.begin(); i != toDo.end(); i++)
{
fputs(*i, f);
delete []*i;
}
fclose(f);
return 0;
}
Comments
Leave a comment