Search code examples
c++c++11run-length-encoding

Why does my RLE code show std out of range for c++?


Whenever I try to run this program, it always shows me the error message

terminate called after throwing an instance of 'std::out_of_range'

What I figured out, when I try to take input as a string then this problem happens. Hence, my loops do not get executed properly.

I really appreciate if anyone can explain me what's wrong with my code!

#include <iostream>
#include <vector>
#include <stdexcept>
#include <string>
using namespace std;

int main()
{
    vector<string> compressed_run_lengths_data;
    vector<char> compressed_characters_data;
    int i;
    int count = 1;
    bool can_be_compressed = false;
    string data;

    try
    {
        cout << "Enter the data to be compressed: ";
        getline(cin, data);

        for (i = 0; i < data.size(); ++i)
        {
            if (!isalpha(data.at(i)))
            {
                throw runtime_error("error: invalid input");
            }
        }

        if (!data.empty())
        {
            i = 1;

            while (i <= data.size())
            {
                if (data.at(i - 1) == data.at(i))
                {
                    count++;

                    if (count > 1)
                    {
                        can_be_compressed = true;
                    }
                }
                else
                {
                    compressed_characters_data.push_back(data.at(i - 1));
                    compressed_run_lengths_data.push_back(to_string(count));
                    count = 1;
                }

                ++i;
            }

            if (can_be_compressed)
            {
                for (i = 0; i < compressed_run_lengths_data.size(); ++i)
                {
                   cout << compressed_run_lengths_data.at(i) << compressed_characters_data.at(i);
                }
            }
            else
            {
               data;
            }         
        }
    }
    catch (runtime_error &e)
    {
        cout << e.what();
        return 1;
    }

    return 0;
}

Solution

  • As requested, an elaboration on my comments:

    while (i <= data.size())                // <- i runs up to and including data.size ()
    {
        if (data.at(i - 1) == data.at(i))   // data.at (i) is out of range when i == data.size ()
    

    I have not analysed your algorithm, but you probably want:

    while (i < data.size())
    

    instead.