Using Arrays
Apr 11, 2017 at 3:09am UTC
I creating an address book program but I need help fixing my existing program to include storing address book entries into an array and a delete user function. Also, how would I change my print record function to display the address book without re-reading the file.
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
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);
}
void MainMenu(ifstream& inData, string filename)
{
char menuOption;
do {
cout << endl;
cout << "Select an option..." << endl;
cout << "(A)dd 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 '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 SearchFirstName(ifstream& inData)
{
string searchName;
entryType userRecord;
string normalSearchName, normalFirstName;
char choice;
bool found = false ;
cout << "Enter first name to search for: " ;
cin >> searchName;
normalSearchName = NormalizeString(searchName); // Convert name to all uppercase
// Loop through all records in the file
while (GetRecord(inData, userRecord)){
normalFirstName = NormalizeString(userRecord.name.firstName); // Convert retrieved string to all uppercase
if (normalFirstName == normalSearchName) { // Requested name matches
PrintRecord(userRecord);
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
if (inData && !found){
cout << "Record found: " << endl;
PrintRecord(userRecord);
cout << endl;
}
else if (!found) // End of file. Name not found.
{
cout << searchName << " not found!" << endl << endl;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
Last edited on Apr 11, 2017 at 1:06pm UTC
Topic archived. No new replies allowed.