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.