Hi,

I'm trying to sort a list of names in c++ (first name and last name). I'm running into a couple of problems with my code. I'll post the code first then state my problems.

#include <iostream>
#include <stdio.h>
#include <cstring>


using namespace std;

const int numNames=3;//this makes the code more flexible, sets the amount of names to sort

int main()
{
    char name [numNames][80];
    char last_name[numNames][80];
    char name_buff [1][80];

    cout << "Please enter 3 names to be sorted.\n";
    cout<< "\nEntires should be of the format\n";
    cout << "First_Name Last_Name." << "\n\n";

    for (int i=0; i<numNames; ++i)//use a loop to get user input
        gets(name[i]);

    cout<< "\n";
    cout<<"***Initialise check***"<<"\n\n";

    for (int i=0; i<numNames; ++i)//this loop will show the the user the data that has been input
        cout << (name[i])<<"\n";

    cout<< "\n***Check Complete***"<< "\n\n";
    cout<< "Press ENTER to contine";
    cin.get();

    // This is the bubble sort.
      for(int a=1; a<=numNames; a++)
      {
        for(int b=numNames-1; b>=a; b--)
        {

          if(name[b-1] [0] > name[b][0])
          { // if out of order exchange elements

            strcpy(name_buff [1],name[b-1]);
            strcpy(name[b-1], name[b]);
            strcpy(name[b], name_buff[1]);
          }
        }
      }
    cout << "\nHere are the names in alphabetical order." << "\n\n";
    for (int i = 0; i < numNames; i++)
        cout << name[i] << "\n";

    return 0;
}

The first problem I'm having is that if the last name is long i.e. more than 5-6 characters the program stops running after I press enter to continue.

The second probelm is that I'm only sorting by the first letter of the first name. I'm not sure how I can alter my code to sort by subsequent letters and possibly last name if first names are the same.

I greatly appreciate any help on the problem, I hope my explanation of my problem is clear. Any questions fire them at me.

Thanks in adavance.

Thanks for the link but my programming skill set is quite small. I haven't had any experience with vectors and to be honest I amn't very comfortable with functions. This why I have written the program in the manner above. Is my program completely wrong? or could I get the outcome I want by writing code like this? Thanks for the reply though much appreciated.

YAJUTBIFSYDHTLHTRP suggestion...

(yet another just use the built in function so you don't have to learn how to really program)

Your comparison is somewhat faulty as indicated below;

if(name[b-1] [0] > name[b][0])
{ // if out of order exchange elements

In the above snippet, you're comparing just the first character of each string. You should use a string comparison function such as strcmp to compare the full string as in comparing name[b-1] to name[b].

Also, your outer and inner loops indicated below totally confuse me. Not sure why you would want to decrement a loop in that manner in a bubble sort. A bubble sort is an example of an exchange sort. This sorting method is essentially a repetitive comparison of two SUCCESSIVE elements and swapping is done if the first element is greater than the second element.

> // This is the bubble sort.
> for(int a=1; a<=numNames; a++)
> {
> for(int b=numNames-1; b>=a; b--)
> {

The outer loop should start at zero and the inner loop should be initialized to the value of the outer loop variable. Both loops should be incremented. The inner loop would be maxed out at numNames-1 and the outer loop would be maxed out at numNames.

The processing in the inner loop would consist of comparing the fullname indexed by the outer loop to the fullname indexed by the innerloop + 1. Swap the variables if the appropriate conditions are met.

This article has been dead for over six months. Start a new discussion instead.