-
pointers to structures
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.
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];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;
}
}
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. Thanks
-
I dont know whats wrong here (dont have much experience with structs), but can I just point something out here that irritates me?
Code:
#define MaxName 40
#define Max 100
Should be changed to
Code:
#define MAXNAME 40
#define MAX 100
to improve clarity. Usually macros (#defined'ed things) are written in caps so you can know that they are macros.
IMO, constants should be the same:
Code:
const int MAX_ENTRIES = 100;
-
Since you wont be performing math on the telephone number, you would probably do better to store it as a string.
Then you could write a function that formats telephone numbers:
Code:
bool format_telephone_number(char* tele_in, char* tele_out)
{
if ( !tele_in || !tele_out ) return false;
int len = strlen(tele_in);
if ( len == 7 )
{
// format xxx-xxxx
for ( int i = 0; i < 3; i++ )
tele_out[i] = tele_in[i];
// output dash
tele_out[3] = '-';
for ( int j = 3, int k = 4; j < 7; j++, k++)
tele_out[k] = tele_in[j];
}
else if ( len == 10 )
{
// format (xxx)xxx-xxxx
// Add your code here, should be
// similar to above
}
else
{
// your hands are tied: just output the same string
strcpy(tele_out,tele_in);
}
return true;
}
//Then, call like this;
int main()
{
char output[MaxSize];
char input[MaxSize];
// read into input the telephone number
if (!format_telephone_number(input,output))
{
// handle error
}
cout<<"Telephone number is"<<output<<endl;
// etc.
return 0;
}
-
oops. I didn't zero terminate that string! Ahh...thats why I use the STL...
Be sure you add the following:
Code:
// format xxx-xxxx
for ( int i = 0; i < 3; i++ )
tele_out[i] = tele_in[i];
// output dash
tele_out[3] = '-';
for ( int j = 3, int k = 4; j < 7; j++, k++)
tele_out[k] = tele_in[j];
// zero terminate the output!
tele_out[8] = '\0';
-
Thanks IfYouSaySo, I understand that part now... I'm still having trouble with the area were Temp is (bottom of code) .. How can I sort the list alphabetically, being that I'm using pointer to pointer to structure?
-
Just out of curiosity would I like to be told what would happen if you didn’t made
Code:
int getDirInfo(Direntry **pde)
pde a pointer to a pointer and why. Because I don't get the importance of this. I get that pde is a pointer to an array of pointers, but does that mean it have to be a pointer to a pointer, and still WHY.
-
I feel the same way!! I could do it much simpler, but my cocky professor wants it that way.... this leaves me clueless. Can anyone help?
-
Thanks Salem.. that helped alot. Now, One more question (I hope it's only one more). After I swap the values to be sorted by alphabetical order, how do I 'cout' the sorted list?
I tried
Code:
for(C=0;C<n;C++)
{ cout<<(*(pde+C))->name<<endl;
cout<<(*(pde+C))->tnum<<endl;}
but it comes out in the same order it was input... any suggestions?
-
The std::map is the thing to use for this type of situation.
-
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;
}
}
-
you appear to be trying to use what's called a bubble sort. To do that you don't compare each name in the array with the smallest name in the array, you compare it with the next one in the array. If the current one is less than the next one you move on. Otherwise you switch. After the first round of comparison the name with largest value has bubbled to the top. And on you go until the whole list is sorted.
Code:
void printSorted(Direntry **pde, int n)
{ int C, I;
//do the sorting
for(C=0;C<(n-1);C++)//how many times through the array
{
for(I=0;I<n-C;I++)// which indexes are we using right now
{
if(strcmp((*(pde+I))->name, (*(pde + I + 1))->name) > 0)
{ Direntry *Temp = (*(pde+I));
(*(pde+I))= (*(pde + I + 1));
(*(pde+I + 1))=Temp;
}
}
}
//print the results of the sort
for(i = 0; i < n; i++)
{
cout << (*(pde + i)).name << " number : " <<
(*(pde + i)).tnum << endl;
}
}