Code doesn't work: Check alphabetical order

This is a discussion on Code doesn't work: Check alphabetical order within the C++ Programming forums, part of the General Programming Boards category; I wrote this function as part of a program that requires a list of names to be organized alphabetically. The ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Code doesn't work: Check alphabetical order

    I wrote this function as part of a program that requires a list of names to be organized alphabetically. The entire program works, except it is hindered by this stupid function. I know there is a simple problem with it, something I've overlooked, but I'm so frustrated that I can't find it. If somebody could just point out where I went wrong, I would greatly appreciate it.

    Code:
    bool comparenames(char name1[], char name2[], int length)
    {
    	//compares two character strings of length 'length'
    	//returns true if name1 is first alphabetically, zero if name2
    	int stat = -1;
    	
    	for(int index = 0; index < length; index++)
    	{
    		if(name1[index]==name2[index]);
    		else if(int(name1[index]) < int(name2[index]))
    		{
    			stat = 1;
    			break;
    		}
    
    		//else if(name1[index] > name2[index])
    		else if(int(name1[index]) > int(name2[index]))
    		{
    			stat = 0;
    			break;
    		}
    		else
    			cout << "error" << endl;
    	}
    	if(stat = 1)
    		return 1;
    	else if(stat = 0)
    		return 0;
    	else
    		return 0;
    }
    The function is supposed to take in two character arrays (name1 and name2) as well as an int 'length'. The length is the used as the maximum length of the two arrays. The two arrays are completely alphabetical, no problems with getting wrong types of data. The function should return true if 'name1' is "less than" 'name2' alphabetically.

    As I said, it's probably something very simple that I've overlooked. I'd appreciate any help you could give.

    ~Moto

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    lexicographical_compare() in the STL will compare names correctly.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Ah, thank you. Unfortunately, I should have pointed out that I'm not using any classes. The requirement (it is a small portion of a homework assignment) is that the code is all written fresh, not using classes, etc. Otherwise the assignment would be very, very easy.

    Thank you, though,
    Moto

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    bool less_than_or_equal_to(char name1[], char name2[], int length)
    {	
    	for(int i = 0; i < length; i++)
    		if(int(name1[index]) > int(name2[index]))
    			return false;
    	return true;
    }
    Haven't tested it, but that's a lot like how I'd do it.

    If that doesn't help, please post your sample output.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Heres the problem with your code:
    Code:
    	if(stat == 1)
    		return 1;
    	else if(stat == 0)
    		return 0;
    	else
    		return 0;

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real problem with your code is that you're not using something like... oh, I don't know... strcmp

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putting a word in alphabetical order.
    By Brewer in forum C Programming
    Replies: 12
    Last Post: 12-16-2006, 04:11 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 03:38 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21