I am a C++ beginner experimenting with strings,arrays,etc...I am trying to create a program that sorts words in alphabetical order (came up with the idea myself). I have been told there are programs out there already like bubble-sort, but I am not cheating I want to make this one on my own. The program below isn't complete, it is only supposed to sort by looking at the first letter, but even then it is stuck in some infinite loop. Any ideas would be appreciated, thanks!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr_size;
cout << "Please enter how many words you would like to sort...\n";
cin >> arr_size;
string * items1 = new string [arr_size];
cout << "Please enter words to sort...\n";
int i_counter;
for (i_counter=0; i_counter < arr_size; i_counter++)
{
cin >> items1[i_counter];
}
for (i_counter=0; i_counter < arr_size; i_counter++)
{
cout << items1[i_counter] << "\n";
}
/* Now let us sort these words */
cout << "Here are the words in alphabetical order \n";
bool chaos=true;
do
{
for(i_counter=0;i_counter < arr_size;i_counter++)
{
string temp;
string temp1;
temp=items1[i_counter];
temp1=items1[i_counter+1];
chaos=0;
if (temp[0] > temp1[0])
{
items1[i_counter]=temp1;
items1[i_counter]=temp;
chaos=1;
}
else if (temp[0] == temp1[0])
{
chaos=1;
// More advanced code here later
}
else
{
break;
}
}
}while(chaos==1);
for (i_counter=0; i_counter < arr_size; i_counter++)
{
cout << items1[i_counter] << "\n";
}
return 0;
}
This post has been edited by AgentSmith: 04 June 2011 - 05:02 PM

New Topic/Question
Reply




MultiQuote










|