Thread: pointers to structures

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Arrow 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
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    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;

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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';
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    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.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  7. #7
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?

  8. #8
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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?

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The std::map is the thing to use for this type of situation.

  10. #10
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    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;     
            
       }
       
    }
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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;
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM