-
-
Save anonymous/94aee437b828856eb329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <istream> | |
#include <string> | |
#include <fstream> | |
using namespace std; | |
int const MAX_NUM_CONTACTS = 2; | |
struct Contacts | |
{ | |
string firstName[MAX_NUM_CONTACTS]; | |
string lastName[MAX_NUM_CONTACTS]; | |
string phoneNumber[MAX_NUM_CONTACTS]; | |
string emailAddress[MAX_NUM_CONTACTS]; | |
}; | |
void addContact(Contacts &Contact, int &numContacts); | |
void showContact(Contacts &Contact, int &numContacts); | |
void saveContact(Contacts &Contact, int &numContacts); | |
int main() | |
{ | |
Contacts Contact; | |
char option; | |
bool keepGoing = true; | |
int numContacts = 0; | |
do | |
{ | |
cout << "MENU\n----------------------\n"; | |
cout << "A: Add a contact\n"; | |
cout << "S: Show Contact\n"; | |
cout << "F: Save all contacts to file\n"; | |
cout << "Q: Quit\n"; | |
cout << "Selection: "; | |
cin >> option; | |
//switch statement menu | |
switch (option) | |
{ | |
case 'a': | |
case 'A': | |
if (numContacts <= MAX_NUM_CONTACTS) | |
{ | |
addContact(Contact, numContacts); | |
} | |
else if (numContacts > MAX_NUM_CONTACTS) | |
{ | |
cout << "You have reached the maximum number of contacts."; | |
} | |
break; | |
case 's': | |
case 'S': | |
showContact(Contact, numContacts); | |
break; | |
case 'f': | |
case 'F': | |
saveContact(Contact, numContacts); | |
break; | |
case 'q': // quit option | |
case 'Q': | |
keepGoing = false; | |
break; | |
default: | |
cout << "Please pick a valid option.\n\n"; | |
break; | |
} | |
} while (keepGoing == true); | |
cin.get(); | |
return 0; | |
} | |
void addContact(Contacts &Contact, int &numContacts) | |
{ | |
//entering contact info | |
cout << "Enter contact's first and last name: "; | |
cin >> Contact.firstName[numContacts] >> Contact.lastName[numContacts]; | |
cout << "Enter contact's phone number: "; | |
cin >> Contact.phoneNumber[numContacts]; | |
cout << "Enter contact's email address: "; | |
cin >> Contact.emailAddress[numContacts]; | |
numContacts++; //incrementing number of contacts | |
} | |
void showContact(Contacts &Contact, int &numContacts) | |
{ | |
string searchFirst, searchLast; | |
cout << "Enter first and last name of contact you wish to show: "; | |
cin >> searchFirst >> searchLast; //getting search parameters for first and last name | |
cout << endl; | |
for (int i = 0; i < numContacts; i++) //incrementing through each contact and.... | |
{ | |
if (searchFirst == Contact.firstName[i] && searchLast == Contact.lastName[i]) // ...checking to see if first and last names match the entered query | |
{ | |
cout << "Contact Found!\n\n"; | |
cout << "Name: " << Contact.firstName[i] << " " << Contact.lastName[i] << endl; | |
cout << "Phone Number: " << Contact.phoneNumber[i] << endl; | |
cout << "Email Address " << Contact.emailAddress[i] << endl << endl; | |
} | |
else if ((searchFirst != Contact.firstName[i] && searchLast != Contact.lastName[i])) | |
{ | |
cout << "No contact found, please try again\n"; | |
showContact(Contact, numContacts); | |
} | |
} | |
} | |
void saveContact(Contacts &Contact, int &numContacts) | |
{ | |
ofstream file; | |
string contactList; | |
cout << "Please enter a file name including the extension to write to: "; | |
cin >> contactList; //inputting name of file | |
file.open(contactList.c_str()); //opening file | |
if (file.is_open()) //checking if file is open | |
{ | |
for (int i = 0; i < numContacts; i++) | |
{ | |
//writing to file | |
file << "Name: " << Contact.firstName[i] << " " << Contact.lastName[i] << endl; | |
file << "Phone Number: " << Contact.phoneNumber[i] << endl; | |
file << "Email Address " << Contact.emailAddress[i] << endl << endl; | |
} | |
file << endl; | |
} | |
else | |
{ | |
cout << "File could not be opened...\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment