Hey guys, I'm having trouble with this program that I have... a telephone directory system whichs allows user to input name and telephone number and in turn sort the list alphabetically.
One other thing... The user inputs the telephone number in consecutive digits like 7321112345. Since it is being stored in a structure, how do i output it like (732)111-2345? It's bugging me!! please help! Any hits are aprreciated. ThanksCode:#include<iomanip.h> #include<string.h> #include<stdlib.h> #define MaxName 40 #define Max 100 struct Direntry { char name[MaxName]; long double tnum; }; int getDirInfo(Direntry **); void printSorted(Direntry **, int ); //void findDisp(Direntry **, int ); main() { int C, I, x,n; Direntry * ade[100]; do{ cout<<"\n\t\t Telephone Directory System \n"; cout<<"\n Enter directory entries .........................1"; cout<<"\n Print the directory sorted by name ..............2"; cout<<"\n Search and display telephone number by name......3"; cout<<"\n Exit the program.................................0"; cout<<"\n\n Please enter your choice :"; cin>>x; switch(x) { case 1: n= getDirInfo(ade); break; case 2: printSorted(ade, n); break; //case 3: findDisp(ade, n); //break; } }while(x != 0); } int getDirInfo(Direntry **pde) { int I,num; cout<<"\n How many entries do you want to make?: "; cin>>num; for(I=0;I<num;I++) { pde[I]= new Direntry; cout<<"\n\n Enter a Name: "; cin.ignore(); cin.getline((*(pde+I))->name,MaxName); cout<<" Enter a phone number: "; cin>>(*(pde+I))->tnum; cout<<(*(pde+I))->name <<" "; cout.setf(ios::fixed); cout<<setprecision(0)<<(*(pde+I))->tnum; } return num; } void printSorted(Direntry **pde, int n) { int C, I, MinIndx; char Min[MaxName];Direntry *Temp[100]; for(C=0;C<(n-1);C++) { Min = (*(pde+C))->name; MinIndx = C; for(I=0;I<n;I++) {if(strcmp(Min,(*(pde+I))->name)<0) { Min=(*(pde +I))->name; MinIndx = I; } } if(strcmp((*(pde+C))->name,Min)<0) //this area is the problem. How do I put the temporary minimum in Temp? being that I'm using pointer to pointer to structure? Should I even do it this way? { Temp[C] = (*(pde+C)); (*(pde+C))= (*(pde+MinIndx)); (*(pde+MinIndx))=Temp[C]; } cout<<(*(pde+C))->name<<endl; cout<<(*(pde+C))->tnum<<endl; } }



LinkBack URL
About LinkBacks



Have a nice day.