Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class phonebook//Base Class
- {
- public:
- string name;
- string mobile;
- string email;
- virtual void modifynumber(string nam){};
- virtual void modifyname(string nam){};
- virtual void display(string nam){};
- virtual void add(string name){};
- virtual void delete_number(string name){};
- virtual void clear_all(){};
- virtual void send_messages(string name){};
- virtual void display_sent_messages(string name){};
- };
- class addnum:public phonebook
- {
- public:
- void add(string name);
- };
- //to display or modify existing number
- class existnum:public phonebook
- {
- void modifynumber(string nam);
- void modifyname(string nam);
- void display(string nam);
- };
- //to delete a particular number
- class deletenum:public phonebook
- {
- void delete_number(string name);
- };
- //to clear the whole phonebook
- class clear_phonebook: public phonebook
- {
- void clear_all();
- };
- void addnum:: add(string name)
- {
- fstream fin;
- //ofstream out;
- fin.open("file1.csv",ios::in | ios::app);
- //out.open("Edited.csv",ios::out);
- vector<string>row;
- string line,word,temp,num;
- cout<<"Enter the Number"<<endl;
- cin>>num;
- cout<<"Want to add email?"<<endl;
- string str;
- if(str=="YES" || "yes")
- {
- cout<<"Enter the email"<<endl;
- string email;
- cin>>email;
- fin<<name<<","<<num<<","<<email<<endl;
- }
- fin.close();
- }
- void clear_phonebook::clear_all()
- {
- remove("file1.csv");
- return;
- }
- void existnum::display(string name)
- {
- fstream fin;
- fin.open("file1.csv",ios::in);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- //getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cnt=1;
- cout<<row[1]<<endl<<row[2]<<endl;
- }
- }
- fin.close();
- if(cnt==0)
- {
- cout<<"Not found"<<endl;
- }
- }
- void existnum:: modifyname(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- cnt=1;
- cout<<"Enter the Modified Name"<<endl;
- string newname;
- cin>>newname;
- fout<<newname<<","<<row[1]<<","<<row[2]<<endl;
- //break;9
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- void existnum:: modifynumber(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- cnt=1;
- cout<<"Enter the Modified number"<<endl;
- string newnum;
- cin>>newnum;
- fout<<row[0]<<","<<newnum<<","<<row[2]<<endl;
- //break;9
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- void deletenum:: delete_number(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- continue;
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- class message :public phonebook
- {
- void send_messages(string name);
- void display_sent_messages(string name);
- };
- void message::send_messages(string name)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("msg.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cout<<"Enter your text."<<endl;
- cout<<"To: "<<endl<<row[0]<<endl<<row[1]<<endl;
- string text;
- // cin>>text;
- getchar();
- getline(cin,text);
- fout<<row[0]<<','<<row[1]<<','<<text<<endl;
- }
- }
- fin.close();
- fout.close();
- }
- void message::display_sent_messages(string name)
- {
- fstream fin;
- fin.open("msg.csv",ios::in);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- //getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cnt=1;
- cout<<row[2]<<endl;
- cout<<endl<<endl;
- }
- }
- fin.close();
- if(cnt==0)
- {
- cout<<"Not found"<<endl;
- }
- }
- int main()
- {
- phonebook *phn;
- existnum ex;
- addnum addnumbers;
- deletenum deletenumbers;
- clear_phonebook clr;
- cout<<"*********** ";
- cout<<"Welcome to Phonebook"<<"***********"<<endl;
- cout<<"Enter Your choice"<<endl;
- message msg;
- string name;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment