1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
//This project will allow you to create an address book in which you can add new contacts, search contacts by last name, display the list of contacts, delete contacts, and of course, exit.
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string first, last, emailAddress, number;
class createBook
{
public :
createBook() //constructor
{
count = 0;
}
void addContact();
void displayAll();
void displayContact(int i); //displays a specific entry within address book
void searchContact();
void deleteEntry();
void sort();
struct contact_struct
{
char firstName [10];
string lastName [10];
char email [35];
char phoneNumber [10];
char age [3];
char fax [10];
string type;
};
contact_struct contacts[100]; //can have up to 100 contacts
unsigned int count;
char removeEntry;
bool lessThan(contact_struct c1, contact_struct c2)
{
return c1.lastName < c2.lastName;
}
};
void sort()
{
std::vector<contact_struct> myvector (contacts, contacts+count);
std::sort(myvector.begin(), myvector.end, lessThan);
for(int i = 0; i < count; i++)
{
contacts[i] = myvector[i];
}
}
void createBook::addContact()
{
string type;
cout << "Entry number " << (count + 1) << " : " << endl;
cout << "Enter First Name: ";
cin >> contacts[count].firstName;
cout << "Enter Last Name: ";
cin >> contacts[count].lastName;
cout << "Enter Email: ";
cin >> contacts[count].email;
cout << "Enter Phone Number: ";
cin >> contacts[count].phoneNumber;
cout << "Is this contact a business contact or personal contact?" << endl;
cout << "Please type Business or Personal: ";
cin >> contacts[count].type;
{
if (contacts[count].type == "Business")
{
cout << "Fax number: ";
cin >> contacts[count].fax;
count++;
}
else if (contacts[count].type == "Personal")
{
cout << "Age: ";
cin >> contacts[count].age;
count++;
}
}
}
void createBook::displayContact(int i)
{
cout << endl << "Contact #" << i + 1 << " : " << endl;
cout << "Type of Contact:" << contacts[i].type << endl;
cout << "First Name: " << contacts[i].firstName << endl;
cout << "Last Name: " << contacts[i].lastName << endl;
cout << "Email: " << contacts[i].email << endl;
cout << "Phone Number: " << contacts[i].phoneNumber << endl;
if (contacts[i].type == "Business")
{
cout << "Fax Number: " << contacts[i].fax << endl;
}
else if (contacts[i].type == "Personal")
{
cout << "Age: " << contacts[i].age << endl;
}
}
void createBook::displayAll()
{
cout << "Number of contacts: " << count << endl;
for (int i = 0; i < count; ++i) displayContact(i);
}
void createBook::searchContact()
{
char lastname[10];
cout << "Enter last name: ";
cin >> lastname;
for (int i = 0; i < count; ++i)
{
if (strcmp(lastname, contacts[i].lastName) == 0)
{
cout << "Found ";
displayContact(i);
cout << endl;
}
}
}
void createBook::deleteEntry()
{
int found = 0;
cout << "Enter the last name of the contact you would like to delete: ";
char lastname[10];
cout << "Enter last name: ";
cin >> lastname;
for (int i = 0; i < count; ++i)
{
if (strcmp(lastname, contacts[i].lastName) == 0)
{
found = i;
break;
}
if (found > 0)
{
for (int index = found; index < count; index++)
{
contacts[i] = contacts[i + 1];
}
}
}
count--;
}
createBook my_book;
int mainMenu()
{
int choice;
bool quit = false;
while (quit == false)
{
cout << "+===================================================+" << endl;
cout << "| Address Book Main Menu |" << endl;
cout << "| |" << endl;
cout << "| (1) - Add a new contact |" << endl;
cout << "| (2) - Search a contact by last name |" << endl;
cout << "| (3) - Display all contacts |" << endl;
cout << "| (4) - Delete Entry |" << endl;
cout << "| (5) - Exit Program |" << endl;
cout << "+===================================================+" << endl;
cout << " " << endl;
cout << "Please enter your choice by inputting a number between 1 and 4." << endl;
cout << "Your choice: ";
cin >> choice;
cout << endl;
if (choice == 1) my_book.addContact();
else if (choice == 2) my_book.searchContact();
else if (choice == 3) my_book.displayAll();
else if (choice == 4) my_book.deleteEntry();
else if (choice == 5)
{
cout << "Thank you for using the program. Goodbye!" << endl;
quit = true;
}
else cout << "Error. Invalid choice. Please try again." << endl;
cout << endl;
}
return 0;
}
int main ()
{
unsigned int count;
mainMenu();
return 0;
}
|