CODE
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
struct WORDS
{
char a[50];
char b[50];
};
bool check;
const int word_number = 5;
void sort_abc(int, WORDS[]);
bool check_abc(WORDS[]);
void wait (float)
//-----------------------------------------------
int main()
{
bool check;
WORDS words[word_number];
int entered;
for(entered = 0; entered < 5; ++entered)
{
cout<<endl<<"Enter a word : ";
cin>>words[entered].a;
strcpy(words[entered].b, words[entered].a);
}
sort_abc(entered, words);
system("PAUSE");
return 0;
}
//-----------------------------------------------
void sort_abc(int entered, WORDS words[])
{
int row = 0;
cout<<"The list alphabetized is :"<<endl;
for(row = 0;check_abc(words) != true; ++row)
{
if(row > 4)
row = 0;
if(strcmp (words[row].a, words[row+1].a) > 0)
{
cout<<"Changing letters : "<<words[row].a<<" & "<<words[row+1].a<<endl;
wait(1);
strcpy(words[row].a, words[row+1].b);
strcpy(words[row+1].a, words[row].b);
}
check_abc(words);
}
cout<<words[0].a<<endl<<words[1].a<<endl<<words[2].a<<endl<<words[3].a<<endl<<words[4].a<<endl;
}
//-----------------------------------------------
bool check_abc(WORDS words[])
{
if(strcmp(words[0].a, words[4].a) < 0 && strcmp(words[1].a, words[4].a) < 0 && strcmp(words[2].a, words[4].a) < 0 && strcmp(words[3].a, words[4].a) < 0)
if(strcmp(words[0].a, words[3].a) < 0 && strcmp(words[1].a, words[3].a) < 0 && strcmp(words[2].a, words[3].a) < 0)
if(strcmp(words[0].a, words[2].a) < 0 && strcmp(words[1].a, words[2].a) < 0)
if(strcmp(words[0].a, words[1].a) < 0)
check = true;
return check;
}
//-----------------------------------------------
void wait(float seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK;
while (clock() < endwait) {}
}
I'm having a huge problem with this. This records 5 words, then sorts them out in alphabetical order.
If you change the order of one or two letters, it works. But if I change the whole thing, it screws up.
I know why one part of it is buggy, it's the
words[row+1].athat goes over 4 and gets a weird number in the memory.