-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.cpp
153 lines (131 loc) · 4.05 KB
/
ToDoList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// libraries used
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits>
using namespace std;
// Project: My Objectives
// Initial Code by: Emadul Emad
// Refined with suggestions from open ai
void addOBJ(vector<string> &objectives);
void saveOBJ(const vector<string> &objectives);
void loadOBJ(vector<string> &objectives);
void displayOBJ(const vector<string> &objectives);
void deletedOBJ(vector<string> &objectives);
int main()
{
vector<string> objectives;
loadOBJ(objectives);
int choice;
do
{
cout << "Welcome to the To-DO list" << endl;
cout << "1. Add objective" << endl;
cout << "2. View current objectives" << endl; // user options
cout << "3. Delete objective/s" << endl;
cout << "4. Exit for now" << endl;
cout << "Please select a choice 1-4: ";
cin >> choice;
if (cin.fail())
{
cin.clear(); // Clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore invalid input
cout << "Invalid choice, try again!" << endl;
continue;
}
switch (choice)
{
case 1:
addOBJ(objectives); // add objectives
break;
case 2:
displayOBJ(objectives); // show current objectives
break;
case 3:
deletedOBJ(objectives); // remove objectives
break;
case 4:
saveOBJ(objectives);
cout << "Exiting..." << endl; // save objectives and exit
break;
default:
cout << "Invalid choice, try again!" << endl; // prints if user does not chose a number 1-4
break;
}
} while (choice != 4);
return 0;
}
void addOBJ(vector<string> &objectives)
{
int numObj;
cout << "How many objectives would you like to add: "; // allows user to add as many objectives as needed
cin >> numObj;
cin.ignore();
for (int i = 0; i < numObj; ++i)
{
string objective;
cout << "Enter objective " << (i + 1) << ": "; // allows user to enter objectives
getline(cin, objective);
objectives.push_back(objective);
}
}
void saveOBJ(const vector<string> &objectives)
{
ofstream myfile("List.txt");
if (!myfile.is_open())
{
cout << "Error opening text file" << endl; // prints objectives to a text file
return;
}
for (const string &objective : objectives)
{
myfile << objective << endl;
}
}
void loadOBJ(vector<string> &objectives)
{
ifstream myfile("List.txt");
if (!myfile.is_open())
{ // checks textfile for objectives
cout << "No objectives found" << endl;
return;
}
objectives.clear(); // Clear existing objectives before loading
string objective;
while (getline(myfile, objective))
{
objectives.push_back(objective);
}
}
void displayOBJ(const vector<string> &objectives)
{
if (objectives.empty())
{
cout << "No objectives to display" << endl; // prints if user has not saved any objectives
return;
}
cout << "Here are your objectives for today:" << endl; // prints users objectives
for (size_t i = 0; i < objectives.size(); ++i)
{
cout << (i + 1) << ". " << objectives[i] << endl;
}
}
void deletedOBJ(vector<string> &objectives)
{
if (objectives.empty())
{
cout << "No objectives are present to remove." << endl; // prints if there are no objectives
return;
}
int index;
cout << "Enter the number of the objective you want to remove: "; // requests user to input the number of objectives they want to remove
cin >> index;
if (index < 1 || index > objectives.size())
{
cout << "Invalid number entered." << endl; // prints if user inputs a invalid number for objectives
return;
}
objectives.erase(objectives.begin() + (index - 1));
cout << "Objective has been successfully removed" << endl; // prints if objective has been removed
}