Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<stdio.h>
- #include<utility>
- #include<string>
- #include<vector>
- using namespace std;
- void insert_contact();
- void search_contact();
- void upORdel(string x);
- void update_contact(string outputName, string outputLine, vector<string>output);
- void delete_contact(string outputName, string outputLine);
- void view_all_contact();
- char f1[]="file1.txt", f2[]="file2.txt";
- int main()
- {
- while(1)
- {
- system("cls");
- system("color A1");
- cout<<"\t\t\t\t\t:::WELCOME TO CONTACT MANAGEMENT APPLICATION:::"<<endl;
- cout<<"\t\t\t\t\t\t\t\t\tCreated by Bits"<<endl<<endl<<endl;
- int c;
- cout<<":::Choice your option:::"<<endl<<endl<<endl;
- cout<<"Type 1 for insert contact."<<endl;
- cout<<"Type 2 for search contact."<<endl;
- cout<<"Type 3 for view all contact."<<endl;
- cin>>c;
- switch(c)
- {
- case 1:
- insert_contact();
- break;
- case 2:
- search_contact();
- break;
- case 3:
- view_all_contact();
- break;
- default:
- cout<<"Entered wrong key"<<endl;
- break;
- }
- }
- return 0;
- }
- void insert_contact()
- {
- system("color D1");
- ofstream FILE;
- FILE.open(f1, ios::app);
- cout<<endl<<"Enter User name: ";
- string x;
- getchar();
- getline(cin, x);
- FILE<<endl<<"#"<<x;
- cout<<"Phone number: ";
- cin>>x;
- FILE<<endl<<x<<"^";
- cout<<"Email address: ";
- cin>>x;
- FILE<<x<<"^";
- cout<<"Address: ";
- cin>>x;
- FILE<<x<<"^"<<endl;
- x.clear();
- FILE.close();
- cout<<endl<<endl<<"User info inserted"<<endl<<endl;
- again:
- cout<<endl<<endl<<"For Home page press home."<<endl;
- string h;
- cin>>h;
- if(h=="home"||h=="HOME"||h=="Home")
- {
- system("cls");
- return;
- }
- else {
- cout<<"Wrong key pressed"<<endl;
- goto again;
- }
- }
- void search_contact()
- {
- system("color B1");
- ifstream FILE;
- FILE.open(f1);
- cout<<"User Name: ";
- string x="#", data, extra;
- getchar();
- getline(cin, extra);
- x.append(extra);
- bool flag=false;
- while(!FILE.eof())
- {
- getline(FILE, data);
- if(x==data)
- {
- flag=true;
- upORdel(x);
- break;
- }
- }
- if(!flag)
- cout<<"Contact not found"<<endl;
- FILE.close();
- }
- void upORdel(string x)
- {
- ifstream FILE;
- FILE.open(f1);
- string data, extra, ex, exx;
- vector<string>output;
- while(!FILE.eof())
- {
- getline(FILE, data);
- if(x==data)
- {
- extra.assign(x, 1, x.size());
- output.push_back(extra);
- getline(FILE, extra);
- int s=0, k=0;
- for(int i=0;i<extra.size();i++)
- {
- if(k==0) k++;
- else if(k==2) k++;
- else if(k==4) k++;
- if(extra[i]=='^')
- {
- k++;
- output.push_back(ex);
- ex.clear();
- }
- else
- {
- //cout<<extra[i];
- exx=extra[i];
- ex.append(exx);
- }
- }
- break;
- }
- }
- FILE.close();
- int j=0;
- cout<<":::Searched result:::"<<endl;
- for(auto&i:output)
- {
- //cout<<"x"<<endl;
- if(j==0) cout<<"User Name: ", j++;
- else if(j==1) cout<<"Contact Number: ", j++;
- else if(j==2) cout<<"Email Number: ", j++;
- else if(j==3)cout<<"Address: ", j++;
- cout<<i<<endl;
- }
- string outputName='#'+output[0];
- output.erase(output.begin());
- string outputLine;
- for(auto&i:output)
- outputLine.append(i+'^');
- up:
- cout<<endl<<endl<<":::Choice your option:::"<<endl;
- cout<<"Press 1 for update"<<endl;
- cout<<"Press 2 for delete the contact"<<endl;
- cout<<"Press 3 for going home"<<endl;
- cout<<"Choose: ";
- int c;
- cin>>c;
- switch(c)
- {
- case 1:
- update_contact(outputName, outputLine, output);
- break;
- case 2:
- delete_contact(outputName, outputLine);
- break;
- case 3:
- system("cls");
- return;
- break;
- default:
- {
- cout<<"Sorry! you've entered a wrong key."<<endl;
- cout<<"Try again."<<endl;
- goto up;
- }
- break;
- }
- }
- void update_contact(string outputName, string outputLine, vector<string>output)
- {
- vector<string>updatevec(3);
- string x, updateName;
- cout<<":::Update:::"<<endl;
- cout<<"For not making change in a specific filed just type null"<<endl;
- for(int i=0;i<4;i++)
- {
- if(i==0)
- {
- cout<<"New name: ";
- getchar();
- getline(cin, x);
- if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
- updateName='#'+x;
- else updateName=outputName;
- }
- else if(i==1)
- {
- cout<<"New Contact Number: ";
- getline(cin, x);
- if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
- updatevec[0]=x+'^';
- else updatevec[0]=output[0]+'^';
- }
- else if(i==2)
- {
- cout<<"New Email Address: ";
- getline(cin, x);
- if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
- updatevec[1]=x+'^';
- else updatevec[1]=output[1]+'^';
- }
- else if(i==3)
- {
- cout<<"New Address: ";
- getline(cin, x);
- if(x.compare("NULL")!=0&&x.compare("null")!=0&&x.compare("Null")!=0)
- updatevec[2]=x+'^';
- else updatevec[2]=output[2]+'^';
- }
- }
- string updateInfo;
- for(auto&i:updatevec)
- updateInfo.append(i);
- cout<<"Make change?"<<endl;
- cout<<"Type yes/no"<<endl;
- cout<<"Choose: ";
- getline(cin, x);
- //cout<<"Change able Name: "<<updateName<<endl;
- //cout<<"Change able info: "<<updateInfo<<endl;
- if(x.compare("YES")==0||x.compare("yes")==0||x.compare("Yes")==0)
- {
- fstream FILE(f1);
- ofstream FILEx(f2);
- //temp.open(f2, ios::out);
- string line;
- while(!FILE.eof())
- {
- if(line==outputName)
- {
- FILEx<<updateName<<endl;
- getline(FILE, line);
- FILEx<<updateInfo<<endl;
- }
- else FILEx<<line<<endl;
- getline(FILE, line);
- }
- FILE.close();
- ofstream file(f1, ios::trunc);
- ifstream reRight(f2);
- string extra;
- while(!reRight.eof())
- {
- file<<extra<<endl;
- getline(reRight, extra);
- }
- reRight.close();
- FILEx.close();
- ofstream file2(f2, ios::trunc);
- file2.close();
- }
- }
- void delete_contact(string outputName, string outputLine)
- {
- cout<<"Are you sure to delete this contact?"<<endl;
- cout<<"Type yes/no."<<endl;
- cout<<"Choose: ";
- string x;
- cin>>x;
- if(x=="yes")
- {
- fstream FILE(f1);
- ofstream FILEx(f2);
- string line;
- while(!FILE.eof())
- {
- if(line==outputName)
- {
- FILEx<<""<<endl;
- getline(FILE, line);
- FILEx<<""<<endl;
- }
- else FILEx<<line<<endl;
- getline(FILE, line);
- }
- FILE.close();
- ofstream file(f1, ios::trunc);
- ifstream reRight(f2);
- string extra;
- while(!reRight.eof())
- {
- file<<extra<<endl;
- getline(reRight, extra);
- }
- reRight.close();
- FILEx.close();
- ofstream file2(f2, ios::trunc);
- file2.close();
- }
- }
- void view_all_contact()
- {
- system("color E1");
- vector<pair<string, vector<string> > >OutputData;
- ifstream FILE(f1);
- string str, x;
- vector<string>vec;
- string ex1, ex2;
- while(!FILE.eof())
- {
- if(str[0]=='#')
- {
- ex1.assign(str, 1, str.size());
- getline(FILE, str);
- for(int i=0;i<str.size();i++)
- {
- if(str[i]=='^') vec.push_back(x), x.clear();
- else x+=str[i];
- }
- OutputData.push_back(make_pair(ex1, vec));
- ex1.clear();
- vec.clear();
- }
- getline(FILE, str);
- }
- FILE.close();
- //sorting
- string extra;
- vector<string>xvec;
- for(int i=1;i<OutputData.size();i++)
- {
- for(int j=1;j<OutputData.size();j++)
- {
- if((OutputData[j-1].first).compare(OutputData[j].first)>0)
- {
- extra=OutputData[j-1].first;
- xvec=OutputData[j-1].second;
- OutputData[j-1]=OutputData[j];
- OutputData[j].first=extra;
- OutputData[j].second=xvec;
- }
- }
- }
- system("cls");
- cout<<endl<<endl<<":::All User Info:::"<<endl;
- int user=0;
- for(auto&i:OutputData)
- {
- cout<<endl<<user+1<<". User Name: "<<i.first<<endl, user++;
- vec=i.second;
- int x=0;
- for(auto&j:vec)
- {
- if(x==0) cout<<"Contact Number: ", x++;
- else if(x==1) cout<<"Email Address: ", x++;
- else if(x==2) cout<<"Address: ", x++;
- cout<<j<<endl;
- }
- cout<<endl;
- }
- again:
- cout<<endl<<endl<<"For Home page press home."<<endl;
- string h;
- cin>>h;
- if(h=="home"||h=="HOME"||h=="Home")
- {
- system("cls");
- return;
- }
- else {
- cout<<"Wrong key pressed"<<endl;
- goto again;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement