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
|
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<fstream.h>
const int capacity = 100; // mximum number of entries
const int namewide = 25; // number of characters in name
const int numwide = 12; // number of characters in number
class phonebook
{
int size; // how many enries are currently used
char contact[capacity][namewide];
char number[capacity][numwide];
public:
phonebook()
{
cout<<"\t\t\t\t";
textcolor(RED);
cprintf("WELCOME TO SJ'S PHONEBOOK ");
}
void add();
void search();
void edit();
void display();
void load();
void save();
};
void phonebook::add()
{
int n;
textcolor(YELLOW);
cprintf( "\nhow many numbers do you want to add\n");
cout<<endl;
cin >> n;
cin.ignore(1000, '\n'); // discard newline from input buffer
for (int i=0; i<n && size<capacity; i++)
{ textcolor(BLUE);
cprintf( "\nenter the name");cout<<'\t'<< i+1 << endl;
cin.getline(contact[size], namewide);
cprintf("enter the number");
cout<<endl;
cin.getline(number[size], numwide);
size++;
}
}
void phonebook::search()
{ int flag;
char j[25];
cout<<"\nenter the contact\n";
gets(j);
for(int i=0; i<=size; i++)
{if(strcmp(j,contact[i])==0)
{cout<<"here is\t"<<contact[i]<<"'s number\n"<<number[i];flag=1;break;} else flag=0;}
if (flag==0) cout<<"\nno match found for\t"<<j; }
void phonebook::edit()
{cout<<"\nwhose number do you wantt to edit?\n";
char k[25];
gets(k);
for(int i=0;i<size;i++)
{if(strcmpi(k,contact[i])==0)
{cout<<"enter the edited number\n";
gets(number[i]);
cout<<"here is the edited contact\n"<<contact[i]<<':'<<number[i];break;}}}
void phonebook::display()
{ cout<<"\ndisplaying all numbers\n";
for (int i=0; i<size; i++)
{cout <<endl<<contact[i] << ':' << number[i] << endl;
}}
void phonebook::save()
{
ofstream fout("phone.txt");
int a;
a=size;
for (int i=0; i<size; i++)
fout << contact[i] << ':' << number[i] << '\n'<<a;
}
void phonebook::load()
{
ifstream infile("phone.txt",ios::in);
infile>>a;
for (int i=0;i<a;i++)
{infile.getline(contact[i],25);
infile.getline(number[i],12);
}a=size;}
int main()
{
clrscr ();
fstream myfile;
myfile.open ("phone.txt", ios::out | ios::app | ios::in);
phonebook t;
t.load();
cout<<endl<<endl;
for(int i=0;i<=10;i++)
{
cout<<"\n what do you want to \n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
char o;
cin>>o;
switch(o)
{
case 's' :t.search();break;
case 'a' :t.add();break;
case 'e' :t.edit();break;
case 'd' :t.display();
case 'q' :t.save();exit(0);
default:cout<<"\nwrong entry\n.Try once more\n"; } }
myfile.close();
return 0;
}
|