I'm trying to write a program that is given the following structures:
struct aPlayer {
string name; // name of player
int wins; // number of wins player has
};
struct aCompetition {
string name; // name of the match
int numPlayers; // number of players in the club
aPlayer player[10]; // list of players in this club
};
From there I want to write a function that will sort the players by name alphabetically. The function declaration would be as follows:
void sortByName(aCompetition & c){}
Note: I would like to do this by only using for loops, while loops, and if statement(s). The only way I could think to compare the two strings would be to compare their ASCII values. I'm not sure how to do that so any input will be greatly appreciated. Thanks!
Assuming this is for homework (and if it's not, doing this by yourself will help you a lot more than just seeing the answer,) I'm just going to give you a few pointers to help you out.
Compare ASCII values:
aPlayer player1, player2;
player1.name = "bill";
player2.name = "john";
if (player1.name[0] < player2.name[0])
{
// True, in this case, because b is less than j on the ascii table.
}
http://www.asciitable.com for the ascii values. I recommend using tolower() on the player names, because capital letters are lower values than lower case letters.
If the first digit is equal, move on to the second: (One way of doing this.)
aPlayer player1, player2;
player1.name = "alfred";
player2.name = "alvin";
// Find which name is shorter using .length() like player2.name.length()
// Loop through this next part for all aPlayers in aCompetition
for (int i = 0; i < shorterName.length(); i++)
{
// Compare ascii values as I showed above.
// If one is larger than the other, swap them.
}