-1

For class I'm working on an encoder program. All it does is it it compares a messagefile character by character with a bookfile, first matching character it finds and sends the position of that character in the bookfile to the coded file. However once it if it has to search for that character again it has to find the next occurrence of that character. For example, let's say I wanted to encode "Hello world" using a bookfile that also said "Hello world". What I should get is a coded file that says "0 1 2 3 4 5 6 7 8 9 10 11", instead what i get is "0 1 2 2 4 5 6 4 8 9 10 11"

    for (unsigned int i = 0; i < messageFileVector.size(); i++)
    {
        asciiValue = messageFileVector[i];

        if (asciiValue < 128 || asciiValue >= 0)
        {
            count = startingPosition[(int)asciiValue];
            for (unsigned int j = (unsigned int)startingPosition[(int)asciiValue]; j < (int)bookFileVector.size(); j++)
            {
                if (count == bookFileVector.size() - 1)
                {
                    count = 0;
                    j = 0;
                }
                if (messageFileVector[i] == bookFileVector[j])
                {
                    startingPosition[(int)asciiValue] = count + 1;
                    codedFile << j << " ";
                    break;
                }   
            }
            count++;
            continue;
        }
        else
        {
            cout << "Error: message has non-ascii characters" << endl;
            codedFile.clear();
            return EXIT_FAILURE;
        }

My best guess is I did something wrong with the iterator, but for the life of me I can't figure out what.

Frankjoww
  • 45
  • 5
  • What kind of encoding is this? – Havenard Mar 25 '16 at 17:08
  • Shouldn't it be `startingPosition[(int)asciiValue] = j+1`? – Scott Hunter Mar 25 '16 at 17:11
  • @ScottHunter works like a charm, thanks a billion – Frankjoww Mar 25 '16 at 17:19
  • @ScottHunter I have another issue. I have it set that the starting position will reset when it reaches the end of the bookfile. however it doesn't work. lets say my message file was "Hello wolrdH" and my bookfile was just "Hello world" I should be getting 0 1 2 3 4 5 6 7 8 9 10 11 0, bu instead I'm getting "0 1 2 2 4 5 6 4 8 9 10 11 12" – Frankjoww Mar 25 '16 at 17:58
  • It doesn't look like you applied the previous fix, and I think there is a typo in one of your files. Maybe you'd be best asking another question? – Scott Hunter Mar 25 '16 at 18:59

1 Answers1

0

startingPosition[(int)asciiValue] should be set to j+1, not count+1.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101