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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct task {
char myClass[100];
char assign[100];
char due[10];
};
void getString(char input[], int num);
void loadTasks(task tasks[], int& numTasks);
void addTask(task from, task to[], int& num);
void saveTasks(task newTask[], const int num);
void listTasks(task tasks[], const int num);
void processCommand(const int com, task tasks[], int& num);
void inputTask(task tasks[], int& num);
void deleteTask(task currentTasks[], const int whatTask, int& num);
int askCommand();
void pause();
//Main Function
int main() {
task myTasks[100];
int numTasks = 0;
int command;
loadTasks(myTasks, numTasks);
do {
command = askCommand();
processCommand(command, myTasks, numTasks);
}while(command != 4);
saveTasks(myTasks, numTasks);
pause();
}
//Reads in a string
void getString(char input[], int num) {
cin.get(input, num, '\n');
while(!cin) {
cin.clear();
cin.ignore(100, '\n');
cout << "Error!! Please enter something else: ";
cin.get(input, num, '\n');
}
cin.ignore(100, '\n');
}
//Loads tasks from file into program
void loadTasks(task tasks[], int& numTasks) {
ifstream inFile("Saved Tasks.txt");
if(!inFile) {
cout << "File failed to open!!\n";
pause();
exit(1);
}
char aClass[100];
char assignment[100];
char date[10];
task aTask;
while(!inFile.eof()) {
inFile.get(aClass, 100, ';');
inFile.ignore(100, ';');
inFile.get(assignment, 100, ';');
inFile.ignore(100, ';');
inFile.get(date, 10, '\n');
inFile.ignore(100, '\n');
strcpy(aTask.myClass, aClass);
strcpy(aTask.assign, assignment);
strcpy(aTask.due, date);
addTask(aTask, tasks, numTasks);
}
inFile.close();
}
//Adds task to the list
void addTask(task from, task to[], int& num) {
strcpy(to[num].myClass, from.myClass);
strcpy(to[num].assign, from.assign);
strcpy(to[num].due, from.due);
num++;
}
void inputTask(task tasks[], int& num) {
char newClass[100], newAssign[100], newDueDate[100];
task newTask;
cout << endl;
cout << "New Task Form\n";
cout << "--- ---- ----\n\n";
cout << "Class: ";
getString(newClass, 100);
cout << "Assignment Description: ";
getString(newAssign, 100);
cout << "Due Date (e.g. 9/25/12): ";
getString(newDueDate, 100);
strcpy(newTask.myClass, newClass);
strcpy(newTask.assign, newAssign);
strcpy(newTask.due, newDueDate);
addTask(newTask, tasks, num);
cout << "Task added Successfully!\n";
}
//Stops the program
void pause() {
char end;
cout << "Enter anything to continue....... ";
cin >> end;
}
//Saves all of the tasks at the end of the program
void saveTasks(task newTask[], const int num) {
ofstream outFile("Saved Tasks.txt");
if(!outFile) {
cout << "File failed to open!!\n";
pause();
exit(1);
}
for(int x = 0; x < num; x++) {
outFile << newTask[x].myClass << ';' << newTask[x].assign << ';' << newTask[x].due << endl;
}
outFile.close();
}
//Deletes a Task
void deleteTask(task currentTasks[], const int whatTask, int& num) {
for(int x = whatTask; x < num; x++) {
strcpy(currentTasks[x - 1].myClass, currentTasks[x].myClass);
strcpy(currentTasks[x - 1].assign, currentTasks[x].assign);
strcpy(currentTasks[x - 1].due, currentTasks[x].due);
}
num--;
cout << "Task number " << whatTask << " deleted successfully\n";
}
//Lists all tasks on screen
void listTasks(task tasks[], const int num) {
if(strcmp(tasks[0].myClass, "") == 0 || num < 0) {
cout << "You do not have any tasks!\n";
}else {
cout << setfill(' ');
cout << " " << "|Class|" << setw(15) << "|Assignment|" << setw(15) << "|Due Date|\n\n";
for(int x = 0; x < num; x++) {
cout << x + 1 << ". " << "|" << tasks[x].myClass << "|" << " " << setw(15) << "|" << tasks[x].assign << "|" << " " << setw(15) << "|" << tasks[x].due << "|" << endl;
}
}
cout << endl;
pause();
}
//Asks for the user to input a command
int askCommand() {
int com;
cout << endl;
cout << "Menu\n";
cout << "----\n";
cout << endl << endl;
cout << "1 " << setfill ('-') << setw(24) << " See list of Tasks\n";
cout << "2 " << setfill('-') << setw(19) << " Add new Task\n";
cout << "3 " << setfill('-') << setw(20) << " Delete a Task\n";
cout << "4 " << setfill('-') << setw(20) << " Save and Quit\n";
cout << endl;
cout << "Choice: ";
cin >> com;
cout << endl;
while(com < 1 || com > 4) {
cin.ignore(100, '\n');
cout << "Please enter either 1, 2, 3, or 4: ";
cin >> com;
cout << endl;
}
while(!cin) {
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid number... please try again: ";
cin >> com;
cout << endl;
while(com < 1 || com > 4) {
cin.ignore(100, '\n');
cout << "Please enter either 1, 2, 3, or 4: ";
cin >> com;
cout << endl;
}
}
cin.ignore(100, '\n');
return com;
}
//Processes the users command and decides what to do with it
void processCommand(const int com, task tasks[], int& num) {
int taskNum;
switch(com) {
case 1:
listTasks(tasks, num);
break;
case 2:
inputTask(tasks, num);
break;
case 3:
cout << "What is the number of the task you want to delete?: ";
cin >> taskNum;
deleteTask(tasks, taskNum, num);
break;
case 4:
cout << "Bye Bye!\n";
break;
}
}
|