Thread: Helpr required sorting phone entries

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Helpr required sorting phone entries

    hey guys i need to sort out some names in a txt file which is in the following format:

    Code:
    Peter		Bladon	598272651
    	Andrew	Sundhu	857695241
    Sean		Hawley	931003752
    Martin	Testrow	212860499
    	Louise	Curtis	927185887
    Alexander	Oddie		157020783
    John		Colman	478545487
    William	Richardson	689461958
    Reece		Jenkins	872692036
    	Brian		Wright	689846491
    so far this is what i have done:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cctype>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    int a[1000];
    int x;
    int k=0;
    int y;
    int z;
    char *p[1000];
    int h;
    char filetosave[20];
    char filetoopen[20];
    char forename[1000];   
    char surname[1000];
    double number[1000];
    const int maximumwords = 100;
    
    int main(){
    //------------------------------------------
    	//Open a file 
        cout << "Please enter the file to open: " << endl;
    	cin >> filetoopen;
    
          ifstream in(filetoopen);
    //Error checking
          if(!in){
    
                cerr << "Failed to open input file " << filetoopen << endl;
    
                exit(1);
    
          }
    
          
    //Read file till new line
          int h = 0;
    
          while(in){                          
    
                if(h >= maximumwords) break;
    
                if(in >> forename[h]&& in >> surname[h] >> number[h]) h++,k++;        
    
    		   } //end if
    
     
    //Formalities
          in.clear();
    
          in.close();
    
    	cout << "The unsorted array is:\n" << endl;
    	
    	         for(y=0;y<k;y++){
    		cout << "Element " << y	<< ": " << forename[y] << endl;
    	} //end for
    
    //	---------------------
    	
    	for(int i=0;i<k;i++){
    		//*p[x]=&a[x];
    		p[i]=forename+i;
    		//cout << "p[x]=a+x=" << p[x] << endl;
    	} //end for
    
    	for(int g=0;g<k-1;g++){
    
    		for(int i=0;i<k-1;i++){
    			
    			char *t;
    			if(*p[i]>*p[i+1]){
    				t=*(p+i);
    				*(p+i)=*(p+i+1);
    				*(p+i+1)=t;
    			} //end if
    		} //end for
    	} //end for
    
    	cout << "\nThe sorted array is:\n" << endl;
    	for(y=0;y<k;y++){
    		cout << "Element" << y	<< ": " << *p[y] << endl;
    	} //end for
    
    	system("PAUSE");
    } //end main
    I can get it to sort numbers out but not characters. When running this code it outputs nothing.
    Wondering if someone can point me in the right direction please on how to fix this.
    PS. Im a newb at C++ its part of a module im doing at uni:S

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    a few problems to fix:

    1) when you compare two strings stored in "char" arrays, it's not sufficient to compare the first characters of the two strings, as done here
    Code:
     *p[i] > *p[i+1]
    , you should use the function "strcmp" to do that.

    2) The dereference "*" in your last cout should not be there. With your code as it is, it only prints the first character out of that string.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    if you want to keep the entries in memory (which I think you do, because you want to sort them), then you will want to use a 2 dimensional array for forenames, sir names and phone numbers.

    Your while(in) loop is not set up properly. Step through it with a debugger to see what it is doing. It's not doing what you think it is.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tell me the errors of this simple phone book ?
    By blogchama in forum C Programming
    Replies: 5
    Last Post: 01-16-2010, 05:42 AM
  2. Replies: 0
    Last Post: 11-22-2009, 11:23 AM
  3. Structs, dynamic memory, and phone book entries
    By wkfcs in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 03:57 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM