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
|
#include<iostream>
#include<cstring>
#include<fstream>
#include<iomanip>
using namespace std;
const int MAX_CHAR = 101;
const int CAPACITY = 50;
struct Item
{
char courseName[MAX_CHAR];
char task[MAX_CHAR];
char dueDate[MAX_CHAR];
};
char readInOption(char prompt[]);
void getItem(Item list[], char prompt1[], char prompt2[], char prompt3[], int maxChar, int& index);
void displayAll(Item list[], int index);
void readFromFile(ifstream in, Item list[], int maxChar, int& index);
void writeToFile(ofstream out, const Item list[], int index);
int main ()
{
char option;
int index = 0;
Item list[CAPACITY];
ifstream inFile;
ofstream outFile;
readFromFile(inFile, list, MAX_CHAR, index);
cout << "Welcome to the assignment database.\n";
do
{
cout << "a. Add a task.\n"
<< "d. Display all tasks.\n"
<< "f. Find a task by course.\n"
<< "q. Quit.\n";
option = readInOption("Please enter an option: (a, d, f, q) ");
switch (option)
{
case 'a':
cout << endl;
getItem(list, "Please enter the course name: ", "Please enter the task name: ", "Please enter the due date: ", MAX_CHAR, index);
break;
case 'd':
cout << endl;
displayAll(list, index);
break;
case 'f':
cout << "f is invoked\n";
break;
}
}
while (option != 'q');
writeToFile(outFile, list, index);
return 0;
}
char readInOption(char prompt[])
{
char option;
cout << prompt;
cin >> option;
tolower(option);
while (option != 'a' && option != 'd' && option != 'f' && option != 'q')
{
cin.ignore(100, '\n');
cout << "Invalid entry.\n"
<< prompt;
cin >> option;
}
cin.ignore(100, '\n');
return option;
}
void getItem(Item list[], char prompt1[], char prompt2[], char prompt3[], int maxChar, int& index)
{
char confirm;
cout << prompt1;
cin.get(list[index].courseName, maxChar, '\n');
cin.ignore(100, '\n');
cout << prompt2;
cin.get(list[index].task, maxChar, '\n');
cin.ignore(100, '\n');
cout << prompt3;
cin.get(list[index].dueDate, maxChar, '\n');
cin.ignore(100, '\n');
cout << "You entered " << list[index].courseName << ", "
<< list[index].task << ", "
<< list[index].dueDate << endl
<< "Is this correct? (y/n)\n";
cin >> confirm;
tolower(confirm);
while (confirm != 'y' && confirm != 'n')
{
cin.ignore(100, '\n');
cout << "Invalid entry. Type y or n.\n";
cin >> confirm;
cin.ignore(100, '\n');
}
if (confirm == 'y')
{
cout << "Item confirmed.\n";
index++;
}
else if (confirm == 'n')
cout << "Item discarded.\n";
cin.ignore(100, '\n');
}
void displayAll(Item list[], int index)
{
int noOfRecords;
cout << setw(20) << left << "Class"
<< setw(30) << left << "Task"
<< setw(20) << left << "Due date" << endl;
for (noOfRecords = 0; noOfRecords < index; noOfRecords++)
{
cout << setw(20) << left << list[noOfRecords].courseName
<< setw(30) << left << list[noOfRecords].task
<< setw(20) << left << list[noOfRecords].dueDate << endl;
}
cout << endl;
}
void readFromFile(ifstream in, Item list[], int maxChar, int& index)
{
in.open("tasks.txt");
while (!in.eof())
{
in.getline(list[index].courseName, maxChar, ';');
in.getline(list[index].task, maxChar, ';');
in.getline(list[index].dueDate, maxChar, ';');
index++;
}
}
void writeToFile(ofstream out, const Item list[], int index)
{
int noOfRecords;
out.open("tasks.txt");
for (noOfRecords = 0; noOfRecords < index; noOfRecords++)
{
out << list[noOfRecords].courseName << ";"
<< list[noOfRecords].task << ";"
<< list[noOfRecords].dueDate << '\n';
}
}
|