title: C++ issue with LZW decoding
body: <p>I'm writing a LZW algorithm and I'm having trouble with decoding the encoded file. The encoding function works fine,the issues is as far as I know in the building of the dictionary in the decoding function. 
  Edit: The size of the decoding dictionary is for 1 bigger than the encoding dictionary. Also the decoding dictionary doesn't seem to contain any string value that has more than 2 characters. For testing I used a .txt file of Alice in wonderland and the first few words are ok but after that everything goes wrong</p>
  
  <p>My encoding function:</p>
  
  <pre><code>void encode(char* path, int vel_s){
      unordered_map&lt;string,int&gt;dict; //dictionary
      int bit_size = ceil(log2(vel_s + 1)); //size of codes - x bits
      //init dictionary
      for(int i = 0; i &lt; 256; i++){
          string k = "";
          k.push_back((unsigned char)i);
          dict[k] = i;
      }
      int count = 256, maxVel = pow(2, bit_size); //counter and max value in bits
      BinReader reader(path);//text to encodede
      BinWriter writer("out.bin");//output file
      string P = "";
      P.push_back(reader.readByte());//read first char
      while(!reader.f.eof()){
          char C = reader.readByte(); //read next char
          if(dict.find(P + C) != dict.end()){ //if in dictionary
              P+=C;
          }else{ //if not in dictionary
              if(count &gt;= maxVel){
                  count = 0;
              }
              int out = dict[P]; //int code of P
              string binString = toBinary(out, bit_size); //binary value of out
              for(int i = 0; i&lt;binString.size();i++){ //write bits
                  if(binString[i]=='1'){
                      writer.writeBit(true);
                  }else{
                      writer.writeBit(false);
                  }
              }
              dict[P + C] = count; //write P+C to dictionary
              count++;
              P = C; //overwrite P
          }
      }
      //write last output
      int out = dict[P];
      string binString = toBinary(out, bit_size);
      for(int i = 0; i&lt;binString.size();i++){
          if(binString[i]=='1'){
              writer.writeBit(true);
          }else{
              writer.writeBit(false);
          }
      }
  }
  </code></pre>
  
  <p>And this is the decoding function:</p>
  
  <pre><code>
  void decompress(char* path, int vel_s){//file path + dictionary max size
      unordered_map&lt;int,string&gt;dict; //dictionary
      int bit_size = ceil(log2(vel_s + 1));//max number of bits in code
      for(int i = 0; i &lt; 256; i++){ //init dictionary
          dict[i] = (unsigned char) i;
      }
      int count = 256; //counter
      vector&lt;bool&gt;B;
      BinWriter writer("out_d.txt"); //output file
      BinReader reader(path); //input file
      int newI, oldI; //new and old value
      for(int i = 0; i &lt; bit_size; i++){ //read code from encoded file of size bit_size
          B.push_back(reader.readBit());
      }
      oldI = convertToInt(B); //convert to integer
      string S = dict[oldI]; //String is dictionary value of old value
      writer.writeByte(S[0]); //write first letter
      string C = ""; //init C
       C =S[0]; //C is first character of S
      int maxVel = pow(2, bit_size); //max value of encoded code
      while(!reader.f.eof()){
          B.clear();//clear vector B for reading bits
          for(int i = 0; i &lt; bit_size; i++){
              B.push_back(reader.readBit());//read next code
          }
          newI = convertToInt(B); //convert bits to int
          if(dict.find(newI) == dict.end()){ //if new value not in dictionary
              S = dict[oldI]; //S is value of old code
              S += C; //concat C to S
          }else{
              S = dict[newI]; //S is value of new code
          }
          for(int k = 0; k &lt; S.size(); k++){ //write S to file
              writer.writeByte(S[k]);
          }
          C = "";
          C += S[0]; //C is first character of S
          dict[count] = dict[oldI] + C; //new input in dictionary
          count++;
          oldI = newI; //old value is new value
      }
  </code></pre>
  
  <p>And the toBinary() and convertToInt() functions that I use for help</p>
  
  <pre><code>string toBinary(int x, int l){
      string temp;
      while(x!=0){
          temp =(x%2==0 ? "0":"1")+temp;
          x/=2;
      }
      while(temp.length() != l){
          temp = "0"+temp;
      }
      return temp;
  }
  
  
  int convertToInt(vector&lt;bool&gt;B){
      int power= B.size()-1,res= 0;
      for(int i = 0; i &lt; B.size(); i++){
          if(B[i]){
              res+=pow(2, power);
          }
          power--;
      }
      return res;
  }
  </code></pre>