C++: Modify the program below to implement the given header file. I\'m not sure
ID: 3803622 • Letter: C
Question
C++: Modify the program below to implement the given header file. I'm not sure how to change my program code to use the header file properly.(Ex: cin>>Person.firstname). Don't change the overall set up of the program.
Header File
struct Person
{
string phone;
string firstname;
string lastname;
};
struct Address
{
string street;
string zip;
};
Program Code
#include <iostream>
#include <fstream>
using namespace std;
void openFile(string, ifstream&);
bool readFile(ifstream&, string&, string&, string&, string&);
void printInfo(string, string, string, string);
void searchFirst(ifstream&);
void searchLast(ifstream&);
void searchAddress(ifstream&);
void searchPhone(ifstream&);
string uppercaseString(string);
void extraSpaces(string&);
int main()
{
string firstName, lastName, address, phone, filename;
ifstream inData;
char searchMethod;
openFile(filename, inData);
do {
cout << "What do you want to search by?" << endl
<< "(F)irst name, (L)ast name, (A)ddress, (P)hone, or e(X)it?: ";
cin >> searchMethod;
searchMethod = toupper(searchMethod);
switch (searchMethod)
{
case 'F':
searchFirst(inData);
break;
case 'L':
searchLast(inData);
break;
case 'A':
searchAddress(inData);
break;
case 'P':
searchPhone(inData);
break;
case 'X':
break;
default:
cout << "Invalid input!" << endl;
break;
}
} while (searchMethod != 'X');
return 0;
}
void openFile(string filename, ifstream& inData)
{
do {
cout << "Enter the name of the file you want to open: ";
cin >> filename;
inData.open(filename.c_str());
if (!inData)
cout << "File not found try again!" << endl;
} while (!inData);
}
void searchFirst(ifstream& inData)
{
string searchName, firstName, lastName, phone, address,
upperSearch, upperFirstName;
cout << "Enter the first name of the person: ";
cin >> searchName;
cout << endl;
upperSearch = uppercaseString(searchName);
while (readFile(inData, firstName, lastName, phone, address))
{
upperFirstName = uppercaseString(firstName);
if (upperFirstName == upperSearch)
break;
}
if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchName << " not found!" << endl << endl;
}
inData.clear();
inData.seekg(0);
}
void searchLast(ifstream& inData)
{
string searchName, firstName, lastName, phone, address,
upperSearch, upperLastName;
cout << "Enter the last name of the person: ";
cin >> searchName;
cout << endl;
upperSearch = uppercaseString(searchName);
while (readFile(inData, firstName, lastName, phone, address))
{
upperLastName = uppercaseString(lastName);
if (upperLastName == upperSearch)
break;
}
if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchName << " not found!" << endl << endl;
}
inData.clear();
inData.seekg(0);
}
void searchAddress(ifstream& inData)
{
string searchAdd, firstName, lastName, phone, address,
upperSearch, upperAdd;
cout << "Enter the address of the person: ";
cin >> searchAdd;
cout << endl;
upperSearch = uppercaseString(searchAdd);
while (readFile(inData, firstName, lastName, phone, address))
{
upperAdd = uppercaseString(address);
if (upperAdd == upperSearch)
break;
}
if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchAdd << " not found!" << endl << endl;
}
inData.clear();
inData.seekg(0);
}
void searchPhone(ifstream& inData)
{
string searchNum, firstName, lastName, phone, address,
upperSearch, upperNum;
cout << "Enter the phone number of the person: ";
cin >> searchNum;
cout << endl;
upperSearch = uppercaseString(searchNum);
while (readFile(inData, firstName, lastName, phone, address))
{
upperNum = uppercaseString(phone);
if (upperNum == upperSearch)
break;
}
if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchNum << " not found!" << endl << endl;
}
inData.clear();
inData.seekg(0);
}
bool readFile(ifstream& inData, string& firstName, string& lastName, string& phone, string& address)
{
getline(inData, firstName, ',');
extraSpaces(firstName);
getline(inData, lastName, ',');
extraSpaces(lastName);
getline(inData, phone, ' ');
extraSpaces(phone);
getline(inData, address, ' ');
extraSpaces(address);
return inData;
}
void printInfo(string firstName, string lastName, string phone, string address)
{
cout << "Name: " << firstName << " " << lastName << endl;
cout << "Phone: " << phone << endl;
cout << "Address: " << address << endl << endl;
}
string uppercaseString(string str)
{
string nString = str;
int i;
for (i = 0; i < str.size(); i++)
{
nString[i] = toupper(str[i]);
}
return nString;
}
void extraSpaces(string& str)
{
int index;
index = str.find_first_not_of(" ");
str.erase(0, index);
index = str.find_last_not_of(" ");
if (string::npos != index)
str.erase(index + 1);
}
Explanation / Answer
There is nothing to implement in your header file. This needs to have a change in overall setup of program Don't change the overall set up of the program which is indicated by line "cin>>Person.firstname".
Currently what you are having is a procedural way of doing things you need to modify your code to have proper object oriented approach and segregate implementation and logic in header and cpp file creating class for function which will have its required functionality.
If this is not what you need please post a new question with actual text of excercise you have.
Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.