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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAXCHAR = 101;
const int CAP = 100;
struct Task
{
char courseName[MAXCHAR];
char taskDescription[MAXCHAR];
char dueDate[MAXCHAR];
};
void menu();
char getCommand();
void exeCommand(Task [], int &, char);
void addItem(Task [], int &);
void showList(Task [], int &);
void findTask(Task [], int &);
void loadData(Task [], int &);
void addItem(ifstream &, Task [], int &);
void saveData(Task [], int &);
int main(void)
{
Task courses[CAP];
int size = 0;
char option;
loadData(courses, size);
do
{
menu();
option = getCommand();
exeCommand(courses, size, option);
}while(tolower(option) != 'q');
}
void menu()
{
cout << "Welcome to my Task List:\n"
<< "(a) to add a task\n"
<< "(s) to show the task list\n"
<< "(f) to find a task by course name\n"
<< "(q) to quit\n";
cout << "Please enter an option: ";
}
char getCommand()
{
char ans;
cin.get(ans);
switch (tolower(ans))
{
case 'a':
case 's':
case 'f':
case 'q':
cin.ignore(100, '\n');
return ans;
default:
cout << "Illegal input.";
cin.clear();
cin.ignore(100, '\n');
system("cls");
menu();
getCommand();
}
}
void exeCommand(Task list[], int &size, char ans)
{
switch(tolower(ans))
{
case 'a':
addItem(list, size);
saveData(list, size);
break;
case 's':
showList(list, size);
break;
case 'f':
findTask(list, size);
break;
case 'q':
cout << "Thanks for using this program goodbye!" << endl;
return;
default:
return;
}
}
void loadData(Task list[], int &size)
{
ifstream inFile;
inFile.open("task.txt");
if(!inFile)
{
cout << "File did not open! Program terminating!" << endl;
exit(1);
}
while(!inFile.eof())
{
addItem(inFile, list, size);
}
}
void addItem(Task list [], int &size)
{
char course[MAXCHAR];
char taskDesc[MAXCHAR];
char date[MAXCHAR];
char junk;
cout << "Enter Course Name (less than 101 characters.): ";
cin.get(course, MAXCHAR, '\n');
cin.get(junk);
cout << "Enter Task Description (less than 101 characters.): ";
cin.get(taskDesc, MAXCHAR, '\n');
cin.get(junk);
cout << "Enter Due Date (mm/dd/yyyy): ";
cin.get(date, MAXCHAR, '\n');
cin.get(junk);
strcpy_s(list[size].courseName, course);
strcpy_s(list[size].taskDescription, taskDesc);
strcpy_s(list[size].dueDate, date);
size++;
}
void addItem(ifstream &inFile, Task list[], int &size)
{
inFile.get(list[size].courseName, MAXCHAR, ';');
inFile.ignore(50, ';');
inFile.get(list[size].taskDescription, MAXCHAR, ';');
inFile.ignore(50, ';');
inFile.get(list[size].dueDate, MAXCHAR, ';');
inFile.ignore(50, '\n');
size++;
}
void showList(Task list[], int &size)
{
int i;
for(i = 0; i < size; i++)
{
cout << left << list[i].courseName << ";"
<< left << list[i].taskDescription << ";"
<< left << list[i].dueDate << endl;
}
}
void saveData(Task list[], int &size)
{
int i = 0;
ofstream outFile;
outFile.open("task.txt");
if(!outFile)
{
cout << "File did not open! Program terminating!" << endl;
exit(1);
}
for(i = 0; i < size; i++)
{
outFile << list[i].courseName << ';'
<< list[i].taskDescription << ';'
<< list[i].dueDate << endl;
}
}
void findTask(Task list[], int &size)
{
int i = 0;
char junk;
char course[MAXCHAR];
cout << "This will allow you to search for a task by it's course name." << endl;
cout << "Enter Course Name (less than 101 characters.): ";
cin.get(course, MAXCHAR, '\n');
cin.get(junk);
if (strcmp(list[i].courseName, course) == 0)
cout << "found it";
}
| |