#include<iostream>
#include <cstring>
#include<string>
#include <iomanip>
#include <map>
#include<fstream>
#include<algorithm>
#include <vector>
//#include <stdlio>
using namespace std;
fstream phonebook;
struct phone
{
char word[50];
char fword[50];
fstream edit;
int id;
string fname;
string fname2;
string sname;
string lname;
int phonenumber;
int mobilenumber;
string email;
};
phone info;
char d,f,r;
string c,g;
void sortingtany2()
{
vector<string> sV;
phonebook.open("c:\\c\\phonebook.txt");
string word;
// separates words in file at a whitespace
// and loads a string vector
while(getline(phonebook,word))
sV.push_back(word);
cout << "Unsorted words:" << endl;
for(int i = 0; i < sV.size(); i++)
cout << sV[i] << endl;
cout << "---------------" << endl;
cout << "Sorted words:" << endl;
sort( sV.begin(), sV.end() );
for(int i = 0; i < sV.size(); i++)
{
cout << sV[i] << endl;
phonebook<<sV[i];
}
}
void edit()
{
string c;
char c1;
cout << "Please type some lines of text. Enter a period to finish:\n";
do {
c=cin.get();
phonebook>>info.fname;
if(c==info.fname)
{
info.fname.clear();
}
} while (c1!='.');
}
void add()
{
phonebook.open("c:\\c\\phonebook.txt",ios::out|ios::app);
cout<<"enter the first name\n";
cin>>info.fname;
cout<<"enter the second name\n";
cin>>info.sname;
cout<<"enter the last name\n";
cin>>info.lname;
cout<<"enter the phone number\n";
cin>>info.phonenumber;
cout<<"enter the moile number\n";
cin>>info.mobilenumber;
cout<<"enter the e-mail\n";
cin>>info.email;
phonebook<<info.fname<<"\t "<<info.sname<<"\t "<<info.lname<<" \t "
<<info.phonenumber<<" \t "<<info.mobilenumber<<" \t "<<info.email<<endl;;
cout<<endl;
phonebook.close();
}
void getdisplay()
{
cout<<"if u wanna display all contacts press 'a'\n"<<"if u wanna display contacts begining with specific letter press 'c'\n";
cin>>r;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
switch(r)
{
case 'a':
{
while(!phonebook.eof())
{
getline(phonebook,c);
cout<<c<<endl;
}
phonebook.close();
break;
}
case 'c':
{
char le;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
cout<<"enter the wanted letter";
cin>>le;
phonebook.seekg(0,ios::beg);
while(getline(phonebook ,c))
{
cout<<"zashgdas";
for(int i=0;i<c.length();i++)
{
cout<<"afa";
if(c[0]>=le)
{
cout<<c;
cout<<"hasgdajk";
break;
}
}
}
phonebook.close();
break;
}
}
}
void getsearch()
{
cout << "Enter your contact:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
int counter =0;
while (!phonebook.eof())
{
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0)
{
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
cout<<info.fword<<" "<<info.fname<<" "<<info.sname<<" "<<info.lname
<<" "<<info.phonenumber<<" "<<info.mobilenumber<<" "<<info.email<<endl;
phonebook.close();
}
void getmodify()
{
cout << "Enter your letter:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
int counter =0;
while (!phonebook.eof()){
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0){
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
info.word==NULL;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook.close();
}
int getdel()
{
char hot;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
ofstream myfile("c:\\c\\1.txt",ios::out);
cout<<"ur u sure u want to delete all contact(y/n)"<<endl;
cin>>hot;
if (hot=='y')
{
phonebook>>info.fname;
while( getline(phonebook,info.fname))
{
cout << info.fword<< "\n";
}
phonebook.close();
myfile.close();
// delete the original file
remove("c:\\c\\phonebook.txt");
// rename old to new
rename("c:\\c\\1.txt","c:\\c\\phonebook.txt");
}
else
{
cout<<"the contact still as it is"<<endl;
}
// all done!
return 0;
}
void del()
{
string del;
cout << "Enter your contact:"<<endl;
cin >> info.word;
phonebook.open("c:\\c\\phonebook.txt",ios::in);
phonebook.clear();
int counter =0;
while (!phonebook.eof())
{
phonebook >> info.fword;
int j=strcmpi(info.fword,info.word);
if (j==0)
{
cout << "contact was found in the file :)"<<endl;
cout<< info.word << endl;
counter ++;
break;
}
}
if (counter == 0)
cout << "contact was not found in the file :(" << endl;
phonebook>>info.fname>>info.sname>>info.lname>>info.phonenumber>>info.mobilenumber>>info.email;
info.fname.clear();
info.fname=' ';
phonebook<<info.fname;
phonebook.close();
}
int main()
{
while(true)
{
cout<<endl<<"1-Adding new contacts"<<endl
<<"2-Displaying contacts information"<<endl
<<"3-Searching for a contact"<<endl
<<"4-Deleting a contact"<<endl
<<"5-Modifying a contact information"<<endl
<<"6-Sort the contacts by name"<<endl
<<"7-exit"<<endl<<endl;
cout<<"enter your choice"<<endl;
cin>>d;
switch(d)
{
case '1':
{
add();
cout<<"if u wann add another contact press 'y' if not press 'n'";
cin>>f;
if(f=='y')
{
add();
}
break;
}
case '2':
{
getdisplay();
break;
}
case '3':
{
getsearch();
break;
}
case '4':
{
int e;
cout<<"enter '1'if u want to delete all contact"<<endl
<<"enter '2' if u want to delete specific contact"<<endl;
cin>>e;
switch (e)
{
case 1:
{
getdel();
break;
}
case 2:
{
del();
break;
}
}
break;
}
case '5':
{
getmodify();
break;
}
case '6':
{
sortingtany2();
break;
}
case '7':
{
return 0;
break;
}
}
}
system ("pause");
return 0;
}