Im supposed to write a program that reads in 10 c strings and will sort them alphabetically using (strcmp(list[h],list[j])>0) which basically says if list h is closer to the start of the alphabet then it is true else it is false here is my code
#include <iostream>
#include <cstdlib>
using namespace std;
// ----------- Function definitions -------------
int main()
{
// -------- Constant declarations --------
// -------- Variable declarations --------
int i,j,h,count;
char list[9][31];
char sorted[9][31];
//--------Main Program Body----------
cout << "****Alphebetical Sorting Program***"<<endl<<endl;
cout<<"please enter 10 strings you wish to sort"<<endl;
for(h=0;h<10;h++){
cin.get(list[h],31);
cin.ignore();
}
for(h=0;h<10;h++){ //move through each array
for(j=h+1;j<9-h;j++){//select every element to compare to the first arrray
if (strcmp(list[h],list[j])>0){//if list h comes after list j,
count++;} //increase the count because list h belongs later on the list
}//end comparisons
strcpy(sorted[count],list[h]); //copy the position of current string into correct position according to count
}//end c string move to next one
cout<<"the sorted list is";
for(h=0;h<10;h++){
cout <<sorted[h];}
cout << endl << endl;
system("PAUSE"); // Pause the output window for reading
return 0;
}
i realize the last bits incredibly criptic so ill breifly explain
basically im trying to say if the 1st element of the list comes after the second element of the list then the count(its proper position) is incremented and moves through the rest of the list until it finds its correct place. eg if "cat" is the first element of the list but "at" and "bat" are before it, when its compared against them count will be incremented twice meaning it belongs in sorted array pos 2
the thing complies properly but freezes after i put in the c strings any advice would be greatly appreciated. thanks!