I'm creating an address book program that allow user to search by first name, last name,phone number and address. The user is prompted to enter a file name and the file is read into an array. I'm having trouble modifying by existing SearchFirstName function to loop through array. I have read over this topic multiple times I'm just not understanding it. Any help would be greatly appreciated. I know there are two ways to fix my code dynamically allocated array of strings using GetRecord (which will have to be reallocated multiple times), or just store it all in one string and then find out how many records there are and then create an array based on its size and manually load the whole thing in a for loop. I'm not sure how I would do this.
File
Susan, Smith, 123 456 789
101 Main Street
Bob, Smith, 567 345 9076
456 Market Street
string bookArray[MAX_RECORDS];
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];
}
}
}
// Searches passed file stream for a first name read from the user
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;
}
elseif (!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);
}