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
|
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
#include <fstream>
#include <cstdlib>
using namespace std;
//prototypes
void printline(char, int);
//global variable
fstream file;
class contact
{
string name;
string mob;
string line;
public:
// Shows all contacts
bool show()
{
if(name == "")
{
getline(file, line);
cout << line + "\n";
return 1;
}
}
//The contact object is initialized by valid values
bool add(string new_name, string new_mob)
{
if(file.is_open())
{
name = new_name;
mob = new_mob;
file << name + " ";
file << mob + "\n";
return 1;
}
}
//delete contact
bool erase(string new_name)
{
}
};
int main()
{
contact person[20];
file.open("MyContacts.txt");
int choice, i, counter;
bool flag;
bool cancel_flag;
string temp_name, temp_mob;
cout << "**** PHONEBOOK ******" << endl;
do
{
cout << "\n\n\n";
printline('-', 20);
cout << "1. Show contacts" << endl
<< "2. Add Contact" << endl
<< "3. Delete Contact" << endl
<< "4. Quit" << endl << endl
<< "Your choice...";
cin >> choice;
system("cls");
printline('-', 20);
cancel_flag = 0;
switch(choice)
{
case 1:
cout << "Showing Contacts" << endl;
printline('-', 20);
for(i=0; i<20; i++)
person[i].show();
//if(person[i].show())
//flag = 1;
//if(!flag)
//cout << "No contacts found!" << endl;
break;
case 2:
cout << "Add New Contact\t\t\t\tpress $ to cancel" << endl;
printline('-', 20);
flag = 1;
cout << "Name: "; cin >> temp_name;
//Cancel operation
if(temp_name=="$")
{
cancel_flag = 1;
break;
}
cout << "Mobile No.: "; cin >> temp_mob;
//Cancel operation
if(temp_mob=="$")
{
cancel_flag = 1;
break;
}
if(cancel_flag)
{
system("cls");
break;
}
//This code adds the contact to phonebook
for(i=0; i<20; i++)
if(person[i].add(temp_name, temp_mob))
{
cout << "Contact added successfully!" << endl;
flag = 1;
break;
}
//if(!flag)
//cout << "Memory full! Delete some contacts first."
//<< endl;
break;
case 3:
cout << "Enter a contact name to delete:"
"\t\t\tpress $ to cancel\n";
cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
if(cancel_flag)
break;
// This code deletes the contact
if(flag)
{
for(i=0; i<20; i++)
if(person[i].erase(temp_name))
{
cout << "Deleted successfully!" << endl;
break;
}
}
break;
case 4:
return 0;
break;
}
} while(1);
getch();
file.close();
return 0;
}
//prints a line
void printline(char ch, int size)
{
for(int i=0; i<size; i++)
cout << ch;
cout << "\n";
}
|