alright here's the code. My problem is in the printSorted function... how do I print the sorted list in alphabetical order?
Code:
#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];
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)
{ Direntry *Temp = (*(pde+C));
(*(pde+C))= (*(pde+MinIndx));
(*(pde+MinIndx))=Temp;
}
cout<<(*(pde+C))->name<<endl; // is this the right way to print the sorted list?? something tells me it's not, but i can't think of any other way.
cout<<(*(pde+C))->tnum<<endl;
}
}