Enum and Struct Program Help

Write a program that will manage a simple address book. The program should allow you to view and search existing entires as well as add new entries. The data should be stored in a file structured so that a person's first name, last name, and phone number are stored on one line separated by commas and the next line has their address.

Additional requirements:

The program should allow the user to make repeated requests until they choose to exit
The program should find multiple matches if necessary
The program should have an option to print out a list that just includes Name and Phone Number
The program should use at least two struct types and one enumerated type

So I made a program that meets most of the requirements above, but I don't know how to implement enum and struct into it. I don't know how to use them and I'm scared that I will mess up my current program. This is the program I have so far.

#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, '\n');
extraSpaces(phone);

getline(inData, address, '\n');
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(" \t");
str.erase(0, index);

index = str.find_last_not_of(" \t");
if (string::npos != index)
str.erase(index + 1);
}
Last edited on
struct Person{
string firstname;
string lastname;
int phonenumber;


};

for struct u can do like that,for the enum i am not very sure about that
Topic archived. No new replies allowed.