-3

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();
}
dizzykooks
  • 11
  • 1
  • 1
    Why not `std::sort` ? – deviantfan Nov 21 '15 at 00:18
  • Btw., apparently you know how to make integer constants with `const int...`, so why you use `#define` for strings? And why not `std::string`? – deviantfan Nov 21 '15 at 00:21
  • 1
    `It works for the first pass but not after.` -- Define "works". And define "not works". – Mike Nakis Nov 21 '15 at 00:28
  • `!ins.eof()` will blow up in your face. Read more: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Nov 21 '15 at 00:55
  • `if (rearrange_count = 0)` is assigning, not testing. use `if (rearrange_count == 0)` – user4581301 Nov 21 '15 at 00:57
  • 1
    `void main()` rarely works. Prefer `int main()` – user4581301 Nov 21 '15 at 00:58
  • I use #define to get information from the file that had the strings on it .. !ins.eof() is what my teacher has taught me .. didn't catch that assignment, thankyou .. void main() is also what my teacher taught me, will keep this in mind .. I define works as the first pass successfully rearranges the arrays alphabetically, but after that it wont rearrange anything – dizzykooks Nov 21 '15 at 17:15

1 Answers1

0

strcpy_s() uses 3 parameters (at least with Visual Studio). The middle parameter is the size of the destination buffer. I don't know why you're not getting an error. You can use strcpy() instead of strcpy_s(), or add the size parameter.

Only 5 if swap statements are needed:

    if (strcmp(list[0], list[2]) > 0)
    {
        strcpy_s(temp, limit, list[2]);
        strcpy_s(list[2], limit, list[0]);
        strcpy_s(list[0], limit, temp);
        rearrange_count += 1;
    }
    if (strcmp(list[1], list[3]) > 0)
    {
        strcpy_s(temp, limit, list[1]);
        strcpy_s(list[1], limit, list[3]);
        strcpy_s(list[3], limit, temp);
        rearrange_count += 1;
    }
    if (strcmp(list[0], list[1]) > 0)
    {
        strcpy_s(temp, limit, list[1]);
        strcpy_s(list[1], limit, list[0]);
        strcpy_s(list[0], limit, temp);
        rearrange_count += 1;
    }
    if (strcmp(list[2], list[3]) > 0)
    {
        strcpy_s(temp, limit, list[2]);
        strcpy_s(list[2], limit, list[3]);
        strcpy_s(list[3], limit, temp);
        rearrange_count += 1;
    }
    if (strcmp(list[1], list[2]) > 0)
    {
        strcpy_s(temp, limit, list[1]);
        strcpy_s(list[1], limit, list[2]);
        strcpy_s(list[2], limit, temp);
        rearrange_count += 1;
    }
rcgldr
  • 23,179
  • 3
  • 24
  • 50
  • I dunno why `strcpy_s` is compiling either, but once I included cstring, it is. Got the cut-off size right, too. At least in GCC it looks like there's a templated overload with some deep magic interpreting the static array's length. – user4581301 Nov 21 '15 at 01:10
  • Just checked. Yup. Undocumented extension. – user4581301 Nov 21 '15 at 01:11
  • I wanted to just use strcpy.. but for some reason visual studio wont build it unless its strcpy_s – dizzykooks Nov 21 '15 at 17:18
  • @dizzykooks - to stop the warnings from Visual Sudio for functions likst strcpy(), put #define _CRT_SECURE_NO_WARNINGS 1 as the very first line, or at least before any include statements in your source file. – rcgldr Nov 22 '15 at 00:51