//===========================================================Start=========================================================
#include<iostream>
#include <cstring>
#include<fstream>
using namespace std;
void Add();
void search();
void Delete();
void update();
void display();
struct student
{
char name[50];
char address[50];
string phone;
};
int main()
{
char c,a,s;
cout<<"press y to add"<<endl;
cin>>a;
if(a=='y')
{
Add();}
else {
cout<<"wrong input"<<endl;
}
cout<<"press y to search"<<endl;
cin>>s;
if(s=='y')
{
search();}
else {
cout<<"wrong input"<<endl;
}
cout<<"Press y for deleting, press z for updating and press d for display\n";
cin>>c;
if(c=='y'){
Delete();
}
else if(c=='z'){
update();
}
else if(c=='d'){
display();
}
else{
cout<<"wrong input"<<endl;
}
return 0;
}
//====================================Adding Values=============================
void Add()
{
student s;
cout<<"Enter name :\n";
cin>>s.name;
cout<<"Enter Address :\n";
cin>>s.address;
cout<<"Enter Phone Number :\n";
cin>>s.phone;
ofstream project;
project.open("project.txt");
project << s.name<<"\n"<<s.address<<"\n"<<s.phone;
project.close();
}
//====================================Searching Values=============================
void search()
{
string mytext;
ifstream project("project.txt");
while(getline (project, mytext )){
cout<<mytext<<"\n";
}
project.close();
//====================================Deleting Values=============================
}
void Delete()
{
string name,readd;
int k,l=0;
cout<<"Enter contact name you want to delete : ";
cin>>name;
ifstream myfile("project.txt");
ofstream
myfile1("fordlt.txt",ios::app);
if(myfile.is_open()){
string line;
while(getline(myfile,line))
{
if((k=line.find(name))!=string::npos){
cout<<endl;
l++;
}
else{
myfile1<<line<<endl;
}
}
ofstream myfile3("project.txt");
myfile3;
myfile3.close();
myfile.close();
myfile1.close();
ifstream read("fordlt.txt");
ofstream
write("project.txt",ios::app);
while(getline(read,readd))
{
write<<read<<endl;
}
read.close();
write.close();
ofstream rewrite("fordlt.txt");
rewrite;
rewrite.close();
if(l==0){
cout<<"Contact Not Found :"<<endl;
}
else{
cout<<"Contact Deleted Successfully: "<<endl;
}
myfile.close();
}
system("PAUSE");
system("cls");
main();
//====================================Updating Values=============================
}
void update()
{
string name, upname;
int k, l=0;
cout<<"update"<<endl;
cin>>name;
ifstream myfile("project.txt",ios::app);
ofstream update("update.txt",ios::app);
if(myfile.is_open()){
string line;
while(getline(myfile, line))
{
if((k=line.find(name))!=string::npos)
{
cout<<"Name : ";
update<<"Name : ";
cin.sync();
getline(cin,upname);
update<<upname<<"\t";
cout<<"phone : ";
update<<"phone : ";
cin.sync();
getline(cin,upname);
update<<upname<<"\t";
cout<<"City : ";
update<<"City :";
cin.sync();
getline(cin,upname);
update<<upname<<endl;
l++;
}
else{
update<<line<<endl;
}
}
myfile.close();
update.close();
ofstream list("project.txt");
list;
list.close();
ifstream upcon("update.txt",ios::app);
ofstream con("project.txt",ios::app);
while(getline(upcon,name)){
con<<name<<endl;
}
upcon.close();
con.close();
ofstream rewrite("update.txt",ios::app);
rewrite;
rewrite.close();
if(l==0)
{
cout<<"Contact not found :"<<endl;
}
else
{
cout<<"Contact updated successfully: "<<endl;
}
myfile.close();
}
system("PAUSE");
system("cls");
main();
}
//====================================Display Values=============================
void display()
{
string mytext;
ifstream project("project.txt");
while(getline (project, mytext ))
{
cout<<mytext<<"\n";
}
project.close();
}
//===========================================================End=========================================================