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