Nov 4, 2013 at 7:27pm UTC
Trying to return to the main menu of adding contact or showing contact after i finished searching a contact. After I use name or phone number or email ID to search for a contact, and after it prints out the info, i want the program to return to the main menu. right now it just keeps repeating the search options. Here's my code:
#include <iostream>
#include <string>
using namespace std;
class PhoneApp {
//attributes
public:
string FirstName;
string LastName;
string PhoneNumber;
string EmailID;
//constructor
PhoneApp() {
FirstName = "";
LastName = "";
PhoneNumber = "";
EmailID = "";
}
//function 1: Adding a contact
void addContact() {
cout << "Enter First Name: " << endl;
cin >> FirstName;
cout << "Enter Last Name: " << endl;
cin >> LastName;
cout << "Enter Phone Number: " << endl;
cin >> PhoneNumber;
cout << "Enter Email ID: " << endl;
cin >> EmailID;
}
//function 2: Display Contact Details
void displayContact() {
cout << "Contact details as follows: " << endl;
cout << FirstName << endl;
cout << LastName<< endl;
cout << PhoneNumber << endl;
cout << EmailID << endl;
}
};
int main ()
{
//create an object of type PhoneApp
PhoneApp myContacts;
while (1) {
cout << "Press 1 to add contact" << endl;
cout << "Press 2 to display contact" << endl;
cout << "Press 3 to search for contact" << endl;
cout << "Press anything else to exit" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: myContacts.addContact();
break;
case 2: myContacts.displayContact();
break;
case 3: {
while (2) {
cout << "Press 4 to search by first name " << endl;
cout << "Press 5 to search by last name " << endl;
cout << "Press 6 to search by phone number " << endl;
cout << "Press 7 to search by Email ID " << endl;
cout << "Press 8 to exit " << endl;
int choice2;
cin >> choice2;
switch (choice2) {
case 4: {cout << "Who are you looking for?" << endl;
string targetF;
cin >> targetF;
if (myContacts.FirstName.compare(targetF) == 0){
cout << "Here is contact information: "<< endl;
myContacts.displayContact();}
else {
cout << "Sorry cannot find contact. :( " << endl;}}
break;
case 5: { cout << "Give me the last name: " << endl;
string targetL;
cin >> targetL;
if (myContacts.LastName.compare(targetL) == 0) {
cout << "Here is contact information: " << endl;
myContacts.displayContact();}
else {
cout << "Sorry cannot find contact. :( " << endl;}}
break;
case 6: {cout << "Give me the phone number: " << endl;
string targetP;
cin >> targetP;
if (myContacts.PhoneNumber.compare(targetP) == 0) {
cout << "Here is contact information: " << endl;
myContacts.displayContact();}
else {
cout << "Sorry cannot find contact. :( " << endl;}}
break;
case 7: {cout << "Give me the Email ID: " << endl;
string targetE;
cin >> targetE;
if (myContacts.EmailID.compare(targetE) == 0) {
cout << "Here is contact information: " << endl;
myContacts.displayContact(); }
else {
cout << "Sorry cannot find contact. :( " << endl;}}
break;
case 8: return (0);
break;
default: exit (0);
}
}
}
default: exit (0);
}
}
system ("pause");
return 0;
}
Last edited on Nov 4, 2013 at 7:32pm UTC