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
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int GLOBAL_SIZE = 150; // (*)
struct MyEntry
{
char firstName[GLOBAL_SIZE];
char lastName[GLOBAL_SIZE];
char contact[GLOBAL_SIZE];
char address[GLOBAL_SIZE];
};
bool addEntry(MyEntry myEntry[GLOBAL_SIZE], int &count)
{
if(count >= GLOBAL_SIZE) return false;
cout << "Entry [" << count + 1 << "] : " << endl;
cout << "Enter first name : "; cin.getline(myEntry[count].firstName, sizeof(myEntry[count].firstName));
cout << "Enter last name : "; cin.getline(myEntry[count].lastName, sizeof(myEntry[count].lastName));
cout << "Enter contact : "; cin.getline(myEntry[count].contact, sizeof(myEntry[count].contact));
cout << "Enter address : "; cin.getline(myEntry[count].address, sizeof(myEntry[count].address));
count++;
return true;
}
bool editEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int count)
{
id--;
if(id < 0 || id >= count) return false;
cout << "Enter first name : "; cin.getline(myEntry[id].firstName, sizeof(myEntry[id].firstName));
cout << "Enter last name : "; cin.getline(myEntry[id].lastName, sizeof(myEntry[id].lastName));
cout << "Enter contact : "; cin.getline(myEntry[id].contact, sizeof(myEntry[id].contact));
cout << "Enter address : "; cin.getline(myEntry[id].address, sizeof(myEntry[id].address));
return true;
}
bool displayEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int count)
{
id--;
if(id < 0 || id >= count) return false;
cout << "Entry [" << id + 1 << "] : " << endl;
cout << "+ First name : " << myEntry[id].firstName << endl;
cout << "+ Last name : " << myEntry[id].lastName << endl;
cout << "+ Contact : " << myEntry[id].contact << endl;
cout << "+ Address : " << myEntry[id].address << endl << endl;
return true;
}
int searchEntry(MyEntry myEntry[GLOBAL_SIZE], string nameTarget, int count)
{
int i;
int N = 0;
for(i = 0; i < count; i++)
{
if(std::string(myEntry[i].lastName) == nameTarget)
{
displayEntry(myEntry, i + 1, count); N++;
}
}
return N;
}
bool deleteEntry(MyEntry myEntry[GLOBAL_SIZE], int id, int &count)
{
int i;
id--;
if(id < 0 || id >= count) return false;
for(i = id; i < count - 1; i++)
{
myEntry[i] = myEntry[i + 1];
}
count--;
return true;
}
int main()
{
int count = 0;
MyEntry myEntry[GLOBAL_SIZE];
int choice;
int contact_id;
bool bQuit = false;
string lastName;
char lastNameHolder[GLOBAL_SIZE];
while(bQuit == false)
{
system("cls");
cout << "+-------------------------------------+" << endl;
cout << "Address Book Menu " << endl << endl;
cout << " 1. Add contact " << endl;
cout << " 2. Edit contact " << endl;
cout << " 3. Delete contact " << endl;
cout << " 4. View contacts " << endl;
cout << " 5. Search addressbook by last name " << endl;
cout << " 6. Exit " << endl;
cout << "+-------------------------------------+" << endl;
cout << "Number of contact records : " << count << endl;
cout << "Enter your choice : ";
if(cin >> choice && choice >= 1 && choice <= 6)
{
cin.ignore();
switch(choice)
{
case 1 :
{
if(!addEntry(myEntry, count))
{
cout << "+ The address book menu is full. Delete some contact record and try again." << endl;
}
else
{
cout << endl;
cout << "The contact record has been successfully added to the list." << endl;
}
}
break;
case 2 :
{
cout << "Enter the contact id : ";
if(cin >> contact_id && (cin.ignore(), editEntry(myEntry, contact_id, count)))
{
cout << "The entry [" << contact_id << "] has been successfully updated." << endl;
}
else
{
if(!cin)
{
cin.clear();
cin.ignore(1000, '\n');
}
cout << "+ Invalid contact id or the contact record does not exist. Please try again." << endl;
}
}
break;
case 3 :
{
cout << "Enter the contact id : ";
if(cin >> contact_id && (cin.ignore(), deleteEntry(myEntry, contact_id, count)))
{
cout << "The entry [" << contact_id << "] has been successfully removed." << endl;
}
else
{
if(!cin)
{
cin.clear();
cin.ignore(1000, '\n');
}
cout << "+ Cannot remove the entry num " << contact_id << ". Please try again." << endl;
}
}
break;
case 4 :
{
cout << "Enter the contact id : ";
if(cin >> contact_id && (cin.ignore(), displayEntry(myEntry, contact_id, count)))
{
}
else
{
if(!cin)
{
cin.clear();
cin.ignore(1000, '\n');
}
cout << "+ Invalid contact id or the contact record does not exist. Please try again." << endl;
}
}
break;
case 5 :
{
int searchCount;
cout << "Enter the contact last name : ";
if(cin.getline(lastNameHolder, sizeof(lastNameHolder)) && (lastName = lastNameHolder, searchCount = searchEntry(myEntry, lastName, count)))
{
cout << "Found " << searchCount << " entries that match the last name" << endl;
}
else
{
if(!cin)
{
cin.clear();
cin.ignore(1000, '\n');
}
cout << "+ The contact record you request does not exist. Please try again." << endl;
}
}
break;
case 6 : bQuit = true; break;
}
}
else
{
if(!cin)
{
cin.clear();
cin.ignore(1000, '\n');
}
cout << "+ Invalid choice. Please try again." << endl;
}
if(bQuit == false)
{
cout << "+ Please press any key to continue. . ."; cin.get();
}
}
cout << endl;
cout << "Thank you for using the program. . ." << endl;
cout << "Please press any key to continue. . ."; cin.get();
return 0;
}
|