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.