Vous êtes sur la page 1sur 6

#include<iostream>

#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;

void clrscr()
{
system("cls");
}

fstream fstr;

class contact{
private:
char name[25];

long long int number[10];


int num_cnt;
long long int landline[10][2];//0-STD_code 1-number
int lnd_cnt;
char address[100];
int type;
char email_id[5][25];
int ei_cnt;
char website[50];
void add_num()
{
clrscr();
if(num_cnt==10)
{
cout<<"Memory is full."<<endl;
getch();
}
else
{
cout<<"Enter the Number"<<endl;
cin>>number[num_cnt];
if(number[num_cnt]/1000000000==0||
number[num_cnt]/1000000000>9)
{
cout<<"Invalid Number"<<endl;
getch();
}
else
num_cnt++;
}
}

void add_land()
{
clrscr();
if(lnd_cnt==10)
{
cout<<"Memory is full."<<endl;
getch();
}
else
{
cout<<"Enter the STD code"<<endl;
cin>>landline[lnd_cnt][0];
cout<<"Enter telephone number"<<endl;
cin>>landline[lnd_cnt][1];

lnd_cnt++;
}
}

void add_type()
{
clrscr();
cout<<"Enter the type of contact:"<<endl;
cout<<"1: Personal"<<endl;
cout<<"2: Business"<<endl;

cin>>type;
if(type!=1&&type!=2)
{
cout<<"Invalid type"<<endl;
type=0;
getch();
}
}

void add_email()
{
clrscr();
if(ei_cnt==5)
{
cout<<"Memory is full."<<endl;
getch();
}
else
{
cout<<"Enter the Email ID"<<endl;
cin>>email_id[ei_cnt++];
}
}

public:

void new_cont()
{
clrscr();
num_cnt=0;
lnd_cnt=0;
ei_cnt=0;
type=0; // unspecified
//address=" ";
//website=" ";

cout<<"Enter Name"<<endl;
cin>>name;
while(true)
{
clrscr();
int tmp;
cout<<name<<endl<<endl;
cout<<"Choose the options to enter further details"<<endl;
cout<<"1: Number"<<endl;
cout<<"2: Landline"<<endl;
cout<<"3: Address"<<endl;
cout<<"4: Contact Type"<<endl;
cout<<"5: Email ID"<<endl;
cout<<"6: Website"<<endl;
cout<<"7: Create Contact"<<endl;
cin>>tmp;

clrscr();
switch(tmp)
{
case 1:
add_num();
break;
case 2:
add_land();
break;
case 3:
cout<<"Enter Address:"<<endl;
gets(address);
break;
case 4:
add_type();
break;
case 5:
add_email();
break;
case 6:
clrscr;
cout<<"Enter Website"<<endl;
break;
case 7:
break;
default:
cout<<"Invalid Input. Try again"<<endl;
}

if (tmp==7)
break;
}
};

char* get_name()
{
return name;
}
/*
long long int* get_num()
{
long long int *p=number;
return p;
}
*/
void print()
{
clrscr();
cout<<"Name: "<<name<<endl;
cout<<"Number:"<<endl;
for(int i=0;i<num_cnt;i++)
{
cout<<" "<<number[i]<<endl;
}
cout<<"Landline Number:"<<endl;
for(int i=0;i<lnd_cnt;i++)
{
cout<<" "<<landline[i]<<endl;
}
cout<<"Address: "<<address<<endl;
cout<<"Email ID"<<endl;
for(int i=0;i<ei_cnt;i++)
{
cout<<email_id[i]<<endl;
}
cout<<"Website"<<endl;
}

};

contact temp;
int count()
{
int tmp=0;
fstr.seekg(0,ios::beg);
while(fstr.eof())
{
fstr.read((char*)&temp,sizeof(contact));
tmp++;
}
return tmp;
}

int main()
{
fstr.open("datafile",ios::binary|ios::in|ios::out);

int num_cont;
int oper=0;
contact temp2;
int boool=0;
char name[25];
fstream fout;
while(true)
{
clrscr();
num_cont=count();
cout<<" Telephone Directory"<<endl<<endl;
cout<<"Number of contacts = "<<num_cont<<endl<<endl;
cout<<"Enter the number of operation you want to perform: "<<endl;
cout<<"1: Create a New Contact"<<endl;
cout<<"2: Search for a Contact"<<endl;
cout<<"3: Delete a Contact"<<endl;
cout<<"4: Print all the Names"<<endl;
cout<<"5: Exit"<<endl;

cin>>oper;
if(oper==5)
{
fstr.close();
break;
}

switch(oper)
{
case 1:
boool=0;
temp.new_cont();
fstr.seekg(0,ios::beg);
fout.open("temp",ios::binary|ios::out);
while(fstr)
{
fstr.read((char*)&temp2,sizeof(contact));
if(strcmp(temp2.get_name(),temp.get_name())==0)
{
fout.write((char*)&temp,sizeof(contact));
}
else if(strcmp(temp2.get_name(),temp.get_name())<0)
{
fout.write((char*)&temp2,sizeof(contact));
}
else if(!boool)
{
fout.write((char*)&temp,sizeof(contact));
boool=1;
fout.write((char*)&temp2,sizeof(contact));
}
else
{
fout.write((char*)&temp2,sizeof(contact));
}
}
fstr.close();
fout.close();

remove("datafile");
rename("temp","datafile");

fstr.open("datafile",ios::binary|ios::in|ios::out);
break;
case 2:
clrscr;
boool=0;
cout<<"Enter the name of the contact to search"<<endl;
gets(name);
fstr.seekg(0,ios::beg);
while(fstr)
{
fstr.read((char*)&temp,sizeof(contact));
if(!strcmp(temp.get_name(),name))
{
temp.print();
boool=1;
break;
}
}
if(boool==0)
{
cout<<"Contact not found"<<endl;
}
break;
case 3:
clrscr;
cout<<"Enter the name of the contact to delete"<<endl;
gets(name);
fout.open("temp",ios::binary|ios::out);
fstr.seekg(0,ios::beg);
while(!fstr.eof())
{
fstr.read((char*)&temp,sizeof(contact));
if(strcmp(temp.get_name(),name))
fout.write((char*)&temp,sizeof(contact));
}
fstr.close();
fout.close();

remove("datafile");
rename("temp","datafile");

fstr.open("datafile",ios::binary|ios::in|ios::out);
break;
case 4:
fstr.seekg(0,ios::beg);
cout<<"Printing the names: "<<endl;
while(fstr)
{
fstr.read((char*)&temp,sizeof(contact));
cout<<" "<<temp.get_name();
}
cin>>name;
break;
default:
cout<<"Invalid Input"<<endl;
}
}
}

Vous aimerez peut-être aussi