Hey all, I'm in the current process of writing a program that can sort words in alphabetical order. Right now I'm just testing it with a small list to see if it works before I use it on another project. I wrote the test in notebook and the list is: sat, cat, rat each on it's own line. What my program should do is look at each word and sort them in alphabetical order so it should read: cat, sat, rat. But what happens when I run the program is it only shows the last word rat, I think I messed up with the logic somewhere but I don't know where. Anyone have any suggestions as to a way to make it so the whole list shows up?
#include <iostream.h>
#include "apstring.h"
#include "apvector.h"
#include <fstream.h>
int main()
{
ifstream inwords;
inwords.open("test.txt");
apvector<apstring>word(100);
int x;
apstring temp="";
bool run=false;
for(x=0; x<word.length()-2;x++)
{
inwords>>word[x];
for(int y=x+1; y<word.length()-1;y++)
{
if(word[x]>word[y])
{
run=true;
temp=word[x];
word[x]=word[y];
word[y]=temp;
run=false;
}
}
}
cout<<temp<<endl;
return 0;
}
Thanks in advance
#include <iostream.h>
#include "apstring.h"
#include "apvector.h"
#include <fstream.h>
int main()
{
ifstream inwords;
inwords.open("test.txt");
apvector<apstring>word(100);
int x;
apstring temp="";
bool run=false;
for(x=0; x<word.length()-2;x++)
{
inwords>>word[x];
for(int y=x+1; y<word.length()-1;y++)
{
if(word[x]>word[y])
{
run=true;
temp=word[x];
word[x]=word[y];
word[y]=temp;
run=false;
}
}
}
cout<<temp<<endl;
return 0;
}
Thanks in advance