I got a question regarding sorting multiple strings that are entered as such, i need to sort them so that if the list would look like this, i just cant seem to get the strings to be swapped, do i need to be using strcpy? Any help would be great in solving this.
WHAT THE PROGRAM SHOULD LOOK LIKE RUNNING
Please enter the 10 strings to sort.
hedge
tomato
apple
cat
peat moss
potting soil
daisy
rose bush
shovel
grass
The sorted strings are:
apple
cat
daisy
grass
hedge
peat moss
potting soil
rose bush
shovel
tomato
Press any key to continue .
MY CODE
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i,j,maxLoc,number;
char temp[31];
char myString[10][31];// says i want 10 strings of 30 characters
cout<<"Please enter the 10 strings to sort. "<<endl;
//gets the 10 strings
cin.get(myString[0],31);
cin.ignore();
cin.get(myString[1],31);
cin.ignore();
cin.get(myString[2],31);
cin.ignore();
cin.get(myString[3],31);
cin.ignore();
cin.get(myString[4],31);
cin.ignore();
cin.get(myString[5],31);
cin.ignore();
cin.get(myString[6],31);
cin.ignore();
cin.get(myString[7],31);
cin.ignore();
cin.get(myString[8],31);
cin.ignore();
cin.get(myString[9],31);
for(i =9; i>0; i--) { // start at the end of the strings
maxLoc = 0;
for(j = 0; j <= i; j++) // Find the largest remaining word
if (strcmp (myString[j],myString[i])<0)
maxLoc = j;
/* Swap the smallest word with the last unsorted value. */
temp = myString[i];
myString[i] = myString[maxLoc];
myString[maxLoc] = temp;
}
system("PAUSE");
return 0;
}