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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#include <iostream>
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;
char Continue();
char Exit();
struct ab
{
std::string Fname;
std::string Lname;
std::string Number;
std::string Email;
std::string Age;
std::string Fax;
};
std::ostream& operator << (std::ostream& os, const ab& data)
{
os << data.Fname << "\n";
os << data.Lname << "\n";
os << data.Number << "\n";
os << data.Email << "\n";
os << data.Age << "\n";
os << data.Fax << "\n";
return os;
}
std::istream& operator >> (std::istream& is, ab& data)
{
std::getline(is,data.Fname);
std::getline(is,data.Lname);
std::getline(is,data.Number);
std::getline(is,data.Email);
std::getline(is,data.Age);
std::getline(is,data.Fax);
return is;
}
class AddressBook
{
ab var;
public:
void AddContact();
void ContactInfo();
int Total();
void Show1();
void SearchContact();
void All();
void Delete();
};
void AddressBook::AddContact()
{
ofstream outFile;
outFile.open("Book1.txt", ios::app);
ContactInfo();
outFile << var;
Continue();
}
void AddressBook::ContactInfo()
{
char info;
char type;
cout << "Will you be adding a personal or business contact? (p/b)\n";
cin >> type;
cout << "Enter your new contact's first name: ";
cin >> var.Fname;
cout << "\nLast name: ";
cin >> var.Lname;
cout << "\nNumber (without spaces): ";
cin >> var.Number;
cout << "\nE-mail: ";
cin >> var.Email;
cin.get(info);
if (type == 'p'||type == 'P')
{
cout << "\nAnd the age: ";
cin >> var.Age;
cout << "Your contact has been created!\n";
var.Fax == "personal contact - no fax number stored\n";
}
else if (type == 'b'||type == 'B')
{
cout << "\nAnd the fax number: ";
cin >> var.Fax;
cout << "Your contact has been created!" << endl;
var.Age == "business contact - no age stored\n";
}
}
int AddressBook::Total()
{
ifstream inFile;
inFile.open("Book1.txt");
inFile.seekg(0, ios::end);
int n;
n = inFile.tellg()/sizeof(var);
return n;
}
void AddressBook::Show1()
{
cout << "\nContact name: " << var.Fname << " " << var.Lname << "\n";
cout << "\nContact phone number: " << var.Number << "\n";
cout << "\nContact e-mail: " << var.Email << "\n";
cout << "\nContact age: " << var.Age << "\n";
cout << "\nContact fax number: " << var.Fax << "\n";
}
void AddressBook::SearchContact()
{
std::string find;
ifstream inFile;
inFile.open("Book1.txt");
cout << "Enter the last name of the contact to search: ";
cin >> find;
int n = Total();//Total() calculates total number of contacts
for (int i = 0; i < n; i++)
{
inFile >> var;
if (find == var.Lname)
{
Show1();
return;
}
else
{
cout << "Contact was not in the address book.";
}
}
inFile.close();
Continue();
}
void AddressBook::All()
{
string line;
ifstream inFile("Book1.txt");
if (inFile.is_open())
{
while (getline (inFile, line))
{
cout << line << endl;
}
inFile.close();
}
else
{
cout << "Unable to open file.";
}
Continue();
}
void AddressBook::Delete()
{
char find[15];
int n,i;
ifstream inFile;
ofstream outFile;
inFile.open("Book1.txt");
outFile.open("Book2.txt");
n = Total();
for (i = 0; i < n; i++)
{
inFile >> var;
outFile << var;
}
inFile.close();
outFile.close();
outFile.open("Book1.txt",ios::trunc);
inFile.open("Book2.txt");
cout << "\nPlease enter the last name of the contact to be deleted:\n";
cin >> find;
for (i = 0; i < n; i++)
{
inFile >> var;
if (find != var.Lname)
outFile << var;
}
outFile.close();
inFile.close();
}
int main()
{
AddressBook Book;
ab var;
char choice;
cout << "=========================ADDRESS BOOK=========================\n";
cout << "What would you like to do?\n\n";
cout << "1. Create New Contact\n\n";
cout << "2. Search for an Existing Contact\n\n";
cout << "3. View All Contacts\n\n";
cout << "4. Delete a Contact\n\n";
cout << "5. Exit\n\n";
cout << "Please enter a choice from 1 - 5 : \n\n" << endl;
cin >> choice;
if (choice == '1')
Book.AddContact();
else if (choice == '2')
Book.SearchContact();
else if (choice == '3')
Book.All();
else if (choice == '4')
Book.Delete();
else if (choice == '5')
Exit();
return 0;
}
char Continue()
{
char answer;
cout << "\nDo you want to continue using the program? (y/n)\n";
cin >> answer;
if (answer == 'y'||answer == 'Y')
{
return main();
}
}
char Exit()
{
char answer;
cout << "Are you sure you want to exit? (y/n) ";
cin >> answer;
if (answer == 'n'||answer == 'N')
{
return main();
}
}
|