C ++ Phone Book Program

Source: Internet
Author: User
/* C ++ phone Program * // * 1. Read and save txt files. * // * 2. add, delete, and modify contacts. * // * 3. You can query the contact number. * // * 4. You can query contacts by phone number. * // * 5. display all contacts. * // * Running environment vs2010 | codeblocks */# include <iostream> # include <string> # include <vector> # include <fstream> # include <set> # include <stdlib. h> using namespace std; # define file_name1 "phonenumin.txt" // use a macro to define the output file name # define file_name2 "phonenumout.txt" class Phonenumtext {public: string name; string num; Phonenumtext (string mz, string hm) {name = mz; num = hm ;}; class all {public: vector <Phonenumtext> phone; bool addnum (s Tring mz, string hm); bool delnum (int bianhao); void displaynum (vector <Phonenumtext >:: iterator iter1); void displayallnum (); int change (int bianhao ); void find1 (string hm); void find2 (string mz); void readfromtxt (); void savetxt () ;}ans; bool all: addnum (string mz, string hm) {Phonenumtext tmp (mz, hm); for (int I = 0; I <phone. size (); I ++) {if (mz = phone [I]. name) {cout <"this contact already exists! "<Endl; return false ;}} phone. push_back (tmp); cout <"add contact" <tmp. name <"successful" <endl; return false;} bool all: delnum (int bianhao) {if (bianhao> phone. size () | bianhao <1) {cout <"No. input error" <endl; return false ;}vector <Phonenumtext >:: iterator iter1; iter1 = phone. begin () + bianhao-1; phone. erase (iter1); return true;} int all: change (int bianhao) {if (bianhao> phone. size () | bianhao <1) {cout <"No. input error" <<Endl; return 0;} vector <Phonenumtext>: iterator iter1; iter1 = phone. begin () + bianhao-1; cout <"Enter the new number to be modified:" <endl; string str; cin> str; iter1-> num = str; return 1;} void all: displayallnum () {if (phone. size () = 0) {cout <"no contact information" <endl; return;} cout <"No. \ t name \ t number" <endl; for (vector <Phonenumtext>: iterator iter1 = phone. begin (); iter1! = Phone. end (); iter1 ++) {displaynum (iter1) ;}} void all: displaynum (vector <Phonenumtext >:: iterator iter1) {cout <(int) (iter1-phone.begin () + 1) <"\ t" <iter1-> name <"\ t "; cout <"\ t" <iter1-> num <"\ t" <endl;} void all: find1 (string hm) {// enter the number to query the contact for (vector <Phonenumtext>: iterator iter1 = phone. begin (); iter1! = Phone. end (); iter1 ++) {if (iter1-> num = hm) cout <"the contact for this number is:" <iter1-> name <endl; return;} cout <"no contact for this number! "<Endl; return;} void all: find2 (string mz) {// enter the contact query number for (vector <Phonenumtext >:: iterator iter1 = phone. begin (); iter1! = Phone. end (); iter1 ++) {if (iter1-> name = mz) cout <"change the contact number to:" <iter1-> num <endl; return;} cout <"no contact number! "<Endl; return;} void all: readfromtxt () {// read ifstream fin (file_name1) from the file; string mz, hm; while (fin> mz> hm) {addnum (mz, hm);} fin. close (); cout <"read to" <phone. size () <"group data" <endl; system ("pause");} void all: savetxt () {// save it to the file ofstream fout (file_name2 ); for (vector <Phonenumtext>: iterator iter1 = phone. begin (); iter1! = Phone. end (); iter1 ++) {fout <"Contact" <"\ t" <iter1-> name <endl; fout <"Number" <"\ t" <iter1-> num <endl;} fout. close (); cout <"Storage completed" <endl;} void menu_add () {// Add contact menu string name, num; cout <"-> Add contact" <endl; cout <"you are adding a contact" <ans. phone. size () + 1 <"contacts" <endl; cout <"Enter the contact name:" <endl; cin> name; cout <"Enter the contact number:"; cin> num; ans. addnum (name, num); return;} void menu_del () {// contact deletion menu cout <"-> Delete contact" <endl; if (ans. phone. size () = 0) {cout <"no contact information, unable to perform the delete operation" <endl; return;} ans. displayallnum (); cout <"Enter the number of the contact to be deleted:" <endl; int EPT; cin> EPT; if (ans. delnum (ipt) cout <"deleted successfully" <endl; else cout <"failed to delete" <endl; return;} void menu_change () {ans. displayallnum (); cout <"-> enter the ID of the contact to be modified" <endl; int x; cin> x; if (ans. change (x) cout <"you have successfully modified the nth" <ans. phone. size () <"contacts" <endl; else menu_change ();} void menu_find () {// query the cout menu <"-> query" <endl; if (ans. phone. size () = 0) {cout <"no contact information, unable to perform the query operation" <endl; return ;}cout <"select the operation you want to perform: "<endl; cout <" \ t1. enter the contact number to query "<endl; cout <" \ t2. enter the contact query number "<endl; cout <"\ t3. return" <endl; int x; string str; cin> x; switch (x) {case 1: cout <"Enter the number of the contact to be queried:" <endl; cin> str; ans. find1 (str); break; case 2: cout <"enter the name of the contact to be queried:" <endl; cin> str; ans. find2 (str); break; case 3: return; default: cout <"incorrect input" <endl ;}} void menu1 () {// Main Menu int EPT; system ("cls"); while (true) {cout <"\ t ******************************" <<endl; cout <"\ t welcome to use your phone book" <endl; cout <"\ t ******************************" <endl <endl; cout <"select the operation you want to perform:" <endl; cout <"1. add a contact "<endl; cout <" 2. delete the contact "<endl; cout <" 3. show all contacts "<endl; cout <" 4. search for "<endl; cout <" 5. modify "<endl; cout <" 6. save to file "<endl; cout <" 7. exit the program "<endl; cin> EPT; switch (EPT) {case 1: menu_add (); break; case 2: menu_del (); break; case 3: ans. displayallnum (); break; case 4: menu_find (); break; case 5: menu_change (); break; case 6: ans. savetxt (); break; case 7: exit (0); break; default: cout <"incorrect input. Please enter" <endl; break ;} system ("pause"); system ("cls") ;}} int main () {int ipt1; string str1; system ("color "); cout <"\ t ******************************" <endl; cout <"\ t welcome to use your phone book" <endl; cout <"\ t ******************************" <endl <endl; retry: cout <"select Data source:" <endl; cout <"1. read "<endl; cout <" 2. enter <endl; cout <"on the keyboard:" <endl; cin> ipt1; switch (ipt1) {case 1: ans. readfromtxt (); break; case 2: break; default: cout <"incorrect input. Please enter" <endl; goto retry; break;} menu1 (); return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.