0

I am trying to implement the algorithm RLE with simple input like:

ddddddddddhhhhhhhhhhhhhhhttttttttttttt

code:

#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

int main() {

    vector<char> read;

    ifstream file;
    file.open("file.txt");

    if (!file) {
        cout << "Unable to open";
    }

    char v;

    while(file>>v) {
        read.push_back(v);
    }

    char x;
    int count=0;

    for(int i=0; i<read.size(); i++) {

        x = read[i];

        if(x != read[++i]) {
          cout << x << "1";
        }

        while(x == read[++i]) {
            count++;
        }
        cout << x << count;

        count = 0;
    }


    return 0;
}

The output I am getting is:

d9d1h12h1t10t1

Please help me with the code.

Update: I have updated the question as I have realized few things.

Plus: This code produced no output, is there anything wrong which I am doing wrong?

char o;
char n; 
int count=0; 

for(int i=0; i<read.size(); i++) { 

   o = read[i]; 
   n = read[++i]; 
while(o == n) { 
      count++; 
} 

   cout << o << count; 

      if(o != n) { 
           cout << o << "1"; 
      } count = 0; 
} 
return 0;
2
  • 2
    The right ways to fix this are either a) learn to use the single-step feature of your debugger and watch what is happening or b) "run" the code on a sheet of paper and see what is happening. "Learn to fish" and you'll be fed with good code for a lifetime. Commented Jan 9, 2018 at 17:24
  • 1
    char will hold all ascii values, but not the extended Unicode characters. It will also hold values -128 to 127 if signed, 0, to 255 if unsigned, which can be used for run length encoding systems. Commented Jan 9, 2018 at 18:29

1 Answer 1

1

This loop:

char x;
int count=0;

for(int i=0; i<read.size(); i++) {

    int j=i;
    x = read[i];

    if(x != read[++j]) {
      cout << x << "1";
    }

    while(x == read[++j]) {
        count++;
    }
    cout << x << count;
}

Has several errors. First, you should use two indices, i and j. i is going through each element of read, but then j is iterating through a subsequence too. What you want is to go through each element only once, and in each case either print or increase the count. However having a for loop and moving the index inside too is not a very good practice, is rather error-prone. Also you have to cout statements that are do not run at the right time (you don't wan to print something on every iteration, only when the character changes). You could do it with a while loop, or using a simpler structure like:

// If there are no characters finish
if (read.empty()) {
    return 0;
}
// Get the first character
char lastChar = read[0];
int count = 1;  //  We have counted one character for now
// Go through each character (note start from 1 instead of 0)
for(int i = 1; i < read.size(); i++) {
    // Get the next char
    char newChar = read[i];
    // If it is different, print the current count and reset the counter
    if (lastChar != newChar) {
        cout << lastChar << count;
        count = 1;
        lastChar = newChar;
    } else {  // Else increase the counter
        count++;
    }
}
// Print the last one
cout << lastChar << count;

return 0;
Sign up to request clarification or add additional context in comments.

1 Comment

How about this?? char o; char n; int count=0; for(int i=0; i<read.size(); i++) { o = read[i]; n = read[++i]; while(o == n) { count++; } cout << o << count; if(o != n) { cout << o << "1"; } count = 0; } return 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.