my first post so please excuse me if I make a mess of things, and let me know where I went wrong, thanks.
I'm trying to sort arrays alphabetically. They are groups of characters each making up a word. It successfully sorts the first pass, but wont do it after for the second and third pass. I can't figure it out seeing as the code does not change. Remember the first pass works perfectly fine. Thanks
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
using namespace std;
const int limit = 10;
const int noWords = 4;
typedef char nametype[limit];
#define in_file "data.txt"
#define out_file "result.txt"
void main()
{
int total_count = 0;
int nonarranged = 0;
int rearrange_count = 0;
char temp[20];
char tempchar;
nametype list[noWords];
nametype tempWord;
ifstream ins;
ofstream outs;
ins.open(in_file);
outs.open(out_file);
each word is maximum of 9 characters, if it is less than 9 the extra characters are blank spaces. I first run for loops to add each character in the data file into a tempWord, after 9 characters I add the null character '\0' and then add the string to the array list.
while (!ins.eof()) //run until end of file
{
for (int i = 0; i < limit - 1; i++)
{
ins.get(tempchar);
tempWord[i] = tempchar;
}
tempWord[limit - 1] = '\0';
strcpy_s(list[0], tempWord);
for (int i = 0; i < limit - 1; i++)
{
ins.get(tempchar);
tempWord[i] = tempchar;
}
tempWord[limit - 1] = '\0';
strcpy_s(list[1], tempWord);
for (int i = 0; i < limit - 1; i++)
{
ins.get(tempchar);
tempWord[i] = tempchar;
}
tempWord[limit - 1] = '\0';
strcpy_s(list[2], tempWord);
for (int i = 0; i < limit - 1; i++)
{
ins.get(tempchar);
if (tempchar != '\n')
tempWord[i] = tempchar;
}
tempWord[limit - 1] = '\0';
strcpy_s(list[3], tempWord);
once all four words in the line are in list. as list[0] list[1] list[2] list[3] I begin sorting with the following code. It works for the first pass but not after.
//rearrange words into alphabetical order
//pass 1
if (strcmp(list[0], list[1]) > 0)
{
strcpy_s(temp, list[1]);
strcpy_s(list[1], list[0]);
strcpy_s(list[0], temp);
rearrange_count += 1;
}
if (strcmp(list[1], list[2]) > 0)
{
strcpy_s(temp, list[1]);
strcpy_s(list[1], list[2]);
strcpy_s(list[2], temp);
rearrange_count += 1;
}
if (strcmp(list[2], list[3]) > 0)
{
strcpy_s(temp, list[2]);
strcpy_s(list[2], list[3]);
strcpy_s(list[3], temp);
rearrange_count += 1;
}
//pass 2
if (strcmp(list[0], list[1]) > 0)
{
strcpy_s(temp, list[1]);
strcpy_s(list[1], list[0]);
strcpy_s(list[0], temp);
rearrange_count += 1;
}
if (strcmp(list[1], list[2]) > 0)
{
strcpy_s(temp, list[1]);
strcpy_s(list[1], list[2]);
strcpy_s(list[2], temp);
rearrange_count += 1;
}
//pass 3
if (strcmp(list[0], list[1]) > 0)
{
strcpy_s(temp, list[1]);
strcpy_s(list[1], list[0]);
strcpy_s(list[0], temp);
rearrange_count += 1;
}
//to calculate how many sentances did not need to be arranged
if (rearrange_count = 0)
nonarranged += 1;
else
{
total_count += rearrange_count;
rearrange_count = 0;
}
for (int i = 0; i < noWords; i++)
cout << list[i];
cout << endl;
}
cout << "number of sorts " << total_count << endl;
cout << "amount not arranged " << nonarranged;
ins.close();
outs.close();
_getch();
}