int main() { string temp; vector<knox> inreadstream; array<vector<knox>,100> dic; fstream fin; fin.open("D:\\list.txt"); char tempest; readinstream(fin,inreadstream); createdic(inreadstream,dic); sort(inreadstream.begin(),inreadstream.end(),[](knox &knox1,knox &knox2)->bool{return (knox1.servicenumber<knox2.servicenumber?true:false);}); cout<<"So, what are you gonna do?"<<'\n'<<"0:Insertion"<<'\n'<<"1:Search by name"<<'\n'<<"2:Search by number"<<'\n'<<"3:Delete by name"<<'\n'<<"4:Show entity"<<'\n'<<"Q to quit"<<endl; while(cin>>tempest) { if('Q'==tempest||'q'==tempest) { cin.setstate(ostream::failbit); continue; } switch(tempest) { case '0':{string temp2,temp3;cout<<"insertion!\n";cout<<"servicenumber?";cin>>temp2;cout<<"name?";cin>>temp3;temp=to_string(totnumber);insertbyname(inreadstream,dic,temp,temp2,temp3);break;} case '1':{cout<<"name?";cin>>temp;searchbyname(dic,temp);break;} case '2':{cout<<"servicenumber?";cin>>temp;searchforstud(inreadstream.begin(),inreadstream.end()-1,temp);break;} case '3':{cout<<"name?";cin>>temp;deletebyname(dic,temp);break;} case '4':{showcase(dic);break;} default:{cout<<"Whatcha doing? Invalid input!"<<endl;cin.clear();} } cout<<"So, what are you gonna do?"<<'\n'<<"0:Insertion"<<'\n'<<"1:Search by name"<<'\n'<<"2:Search by number"<<'\n'<<"3:Delete by name"<<'\n'<<"4:Show entity"<<'\n'<<"Q to quit"<<endl; } cout<<"Bye!"<<endl; return 0; }
/* * main function */ void main() { char *more; char option; int err; bool done = false; char first[11]; char last[11]; char phone[11]; struct phonebook *entry = NULL; struct phonebook *lastentry = NULL; /* * Allocate memory */ more = (char *)malloc(sizeof(char) * 2); if(more == NULL) { printf("Failed to allocate memory"); return; } printf("Do you want to enter data to the phone book. (y/n): \n"); // fgets(more, sizeof(more), stdin); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ get_input_from_console(more); while(more[0] == 'y' || more[0] == 'Y') { /* * Allocate memory for new entry and check if allocation succeded */ entry = (struct phonebook *)malloc(sizeof(struct phonebook)); if(entry == NULL) { printf("Failed to allocate memory"); return; } /*Enter the information for this entry*/ printf("Enter the first name: \n"); // fgets(entry->fname, sizeof(entry->fname),stdin); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ get_input_from_console(entry->fname); printf("Enter the last name: \n"); // fgets(entry->lname, sizeof(entry->lname),stdin); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ get_input_from_console(entry->lname); printf("Enter the phone number: \n"); // fgets(entry->phone, sizeof(entry->phone),stdin); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ get_input_from_console(entry->phone); entry->next = NULL; /*Add this entry to the linked list*/ if(firstentry == NULL) { firstentry = entry; } else { lastentry->next = entry; } lastentry = entry; printf("Do you want to enter data to the phone book. (y/n): \n"); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ // fgets(more, sizeof(more), stdin); // err = fflush(stdin); /*fflush() flushes stray newline char left in buffer*/ get_input_from_console(more); } /* * Now ask the user if he/she wants to search by name/number */ while(!done) { printf("A: Search by first name and last name.\n"); printf("B: Search by phone number.\n"); printf("*: Quit.\n"); option = getchar(); err = fflush(stdin); /*gets rid of the stray newline char in stdin buffer*/ switch(tolower(option)) { case 'a': /*searchbyname*/ printf("Enter the first name: \n"); err = fflush(stdin); fgets(first, sizeof(first),stdin); err = fflush(stdin); printf("Enter the last name: \n"); fgets(last, sizeof(last),stdin); err = fflush(stdin); entry = NULL; entry = searchbyname(first, last); if(entry != NULL) { printf("Phne number is: %s\n", entry->phone); } else { printf("Could not find the relevant entry.\n"); } break; case 'b': /*searchbynumber*/ printf("Enter the phone number: "); fgets(phone, sizeof(phone),stdin); err = fflush(stdin); entry = NULL; entry = searchbynumber(phone); if(entry != NULL) { printf("This phne belongs to %s %s\n", entry->fname, entry->lname); } else { printf("Could not find the relevant entry.\n"); } break; default: /*quit*/ done = true; break; } } }