Thread: Help me sort the names in alpha order.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Help me sort the names in alpha order.

    One lousy syntax error I cannot fix in a qsort

    I am using a qsort to sort 12 names in alphabetical and ascending order but this error in the prototype stops everything, I cannot fix it.
    Another question. From all the books I have read on C++, not one gives an example on how to search an array of string for a particular word; it looks like all the examples are geared to searching a numeric array. Any good search examples for character arrays?
    Thank you.
    Here are my codes and the error.
    --- - - - - - - - - - -
    -----Configuration: alphabetizeName - Win32 Debug-----------
    Compiling...
    alphabetizeName.cpp
    c:\documents\desktop\badtest\alphabetizename.cpp(5 6) : error C2059: syntax error : ')'
    Error executing cl.exe.
    alphabetizeName.obj - 1 error(s), 0 warning(s)

    The codes:
    /*
    first tested 2/2/02
    Purpose: Sort in alphabetical order a list of strings of string array.
    */

    #include <iostream>
    #include <cstring>
    using namespace std;
    #define const elems 12
    void showArray(char strings[][17], int);

    int main()
    {
    char (strings[12][17]) = {
    "Carl Billion", "Smith Birt", "Al Jim",
    "Albert Jim", "Stan Karty", "Rose Amre",
    "Bay Terr", "Johnson Erab", "Allison Carr",
    "Jean Joey", "Will Bill", "James Gart"
    };
    cout << "The unsorted names are: \n\n";
    showArray(strings, 12);


    qsort( strings, 12, 17, (int (*) ( const void*, const void*) )strcmp);
    for( int i = 0; i < 12; i++ )
    // puts( strings[i] );
    cout << (strings[i]) << endl;
    return 0;
    }


    // function definitions ==========================
    void showArray(char strings[12][17], int elems)
    {
    for (int i = 0; i < elems; i++)
    {
    for ( int j = 0 ; j < elems; j++)
    cout << strings[i][j]<<" ";
    cout << endl;
    }

    cout<<endl<<endl;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try -

    Code:
    #include <iostream> 
    #include <cstring> 
    #include <cstdlib>
    using namespace std; 
    
    void showArray(char strings[][17], int); 
    typedef int (*cmpfunc)(const void*,const void*);
    
    int main() 
    { 
    
    	char (strings[12][17]) = { 
    	"Carl Billion", "Smith Birt", "Al Jim", 
    	"Albert Jim", "Stan Karty", "Rose Amre", 
    	"Bay Terr", "Johnson Erab", "Allison Carr", 
    	"Jean Joey", "Will Bill", "James Gart" 
    	}; 
    	
    	cout << "The unsorted names are: \n\n"; 
    	showArray(strings, 12); 
    
    	qsort( strings, 12, 17, (cmpfunc)strcmp); 
    	
    	for( int i = 0; i < 12; i++ ) 
    		cout << (strings[i]) << endl; 
    
    	char* found = (char*)bsearch("Will Bill",strings,12,17,(cmpfunc)strcmp);
    
    	cout << "FOUND: " <<  found << '\n';
    
    	return 0; 
    } 
    
    
    // function definitions ========================== 
    void showArray(char strings[12][17], int elems) 
    { 
    	for (int i = 0; i < elems; i++) 
    	{ 
    		for ( int j = 0 ; j < elems; j++) 
    			cout << strings[i][j]<<" "; 
    		cout << endl; 
    	} 
    
    	cout<<endl<<endl; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2008, 06:23 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  4. Using quicksort and radix sort for an anagram program
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 09:33 AM
  5. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM