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
|
int main()
{
entryType userRecord;
string filename;
ifstream inData;
char searchOption;
OpenFile(filename, inData);
MainMenu(inData, filename);
return 0;
}
void OpenFile(string& filename, ifstream& inData)
{
do {
cout << "Enter file name to open: ";
cin >> filename;
inData.open(filename.c_str());
if (!inData)
cout << "File not found!" << endl;
} while (!inData);
if(inData.is_open())
{
for(int i=0; i<MAX_RECORDS;i++)
{
inData >> bookArray[i].name.firstName;
}
}
}
void MainMenu(ifstream& inData, string filename)
{
char menuOption;
do {
cout << endl;
cout << "Select an option..." << endl;
cout << "(A)dd entry, (D)elete entry, (P)rint, (S)earch, e(X)it: ";
cin >> menuOption;
menuOption = toupper(menuOption);
switch (menuOption) {
case 'A':
AddEntry(inData, filename);
break;
case 'P':
PrintAddressBook(inData);
break;
case 'S':
SearchMenu(inData);
break;
case 'D':
DeleteEntry(inData);
break;
case 'X':
break;
default:
cout << "Invalid option selected!" << endl;
break;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
} while (menuOption != 'X');
}
void AddEntry(ifstream& inData, string filename)
{
entryType userRecord;
ofstream outData;
ReadRecord(userRecord);
inData.close();
outData.open(filename.c_str(), fstream::app);
WriteRecord(outData, userRecord);
outData.close();
inData.open(filename.c_str());
}
void PrintAddressBook(ifstream& inData)
{
entryType userRecord;
char choice;
cout << "Do you want to print a brief list? (Y/N)";
cin >> choice;
choice = toupper(choice);
if (choice == 'N') {
// Loop through all records in the file
while (GetRecord(inData, userRecord)){
PrintRecord(userRecord);
cout << endl;
}
}
else {
while (GetRecord(inData, userRecord)){
PrintBriefRecord(userRecord);
cout << endl;
}
}
}
void SearchMenu(ifstream& inData)
{
char searchOption;
do {
cout << endl;
cout << "Enter how you want to search... " << endl;
cout << "(F)irst name, (L)ast name, (A)ddress, (P)hone, e(X)it: ";
cin >> searchOption;
searchOption = toupper(searchOption);
switch (searchOption) {
case 'F':
SearchFirstName(inData);
break;
case 'L':
SearchLastName(inData);
break;
case 'A':
SearchAddress(inData);
break;
case 'P':
SearchPhone(inData);
break;
case 'X':
break;
default:
cout << "Invalid option selected!" << endl;
break;
}
} while (searchOption != 'X');
}
// Searches passed file stream for a first name read from the user
void SearchFirstName(ifstream& inData)
{
entryType userRecord;
string searchName;
string normalSearchName, normalFirstName;
char choice;
bool found = false;
cout << "Enter first name to search for: ";
cin >> searchName;
for (int i=0;i<MAX_RECORDS;i++)
{
normalFirstName = NormalizeString(bookArray[i].name.firstName);
if (normalFirstName == normalSearchName) { // Requested name matches
PrintRecord(bookArray[i].name.firstName);
cout << "Is this the correct entry? (Y/N)";
cin >> choice;
choice = toupper(choice);
cout << endl;
if (choice == 'Y') {
found = true;
break;
}
}
}
// Matching name was found before the end of the file
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
|