Thread: compression algorithm outputting wrong data

  1. #1
    Registered User
    Join Date
    Aug 2018

    compression algorithm outputting wrong data

    Hello coders,
    The code is supposed to go through the string and every time there are similar characters it's supposed to increment the counter. An input of "303030404040" is supposed to print out "303403" but its printing bogus data!! please help
    #include <iostream>   // std::cout
    #include <iomanip>
    #include <string>     // std::string, std::to_string
    #include <stdlib.h>     /* atof */
    #include <math.h>
    #include <cstring>
    #include <vector>
    #include <array>
    #include <string>
    #include <algorithm>
    string integerrunlengthencoding(string hextext) {
    int counter=1;
        int length = hextext.size(); 
        string output = "";
        for(int i=0;i<length -1;i=i+2){
            for(int k=1;k<length;k=k+2){
                if(hextexts[i] == hextexts[i+2]){
                    if(hextexts[k] == hextexts[k+2] ){
                    } else{
                        output = output + hextexts[i] + hextexts[k] + std::to_string(counter);
                        counter =1;                    
        output = output + hextexts[length -1] + hextexts[length] + 
        return output.size() < hextext.size() ? output : hextext; 
    int main(){
    string t = "303030404040";
    cout << integerrunlengthencoding(t);
    Last edited by ikhram; 1 Week Ago at 03:10 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Using two for loops seems wrong to me.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I guess showing you how to debug code was a waste of time then.
    comparing characters in a string

    Hack-and-post whenever it doesn't work is not a strategy that will work for you long term.
    You NEED to figure out how to use tools like debuggers to fix your own mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compression algorithm help
    By bhargav_g in forum C Programming
    Replies: 2
    Last Post: 02-24-2013, 09:34 AM
  2. Working with LZW data compression algorithm
    By husslela2 in forum C Programming
    Replies: 3
    Last Post: 07-01-2010, 09:23 AM
  3. Ziv-Lempel data compression algorithm
    By ataman in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2008, 03:41 PM
  4. Simple compression algorithm?
    By cyberfish in forum Tech Board
    Replies: 8
    Last Post: 05-02-2008, 09:10 AM
  5. what's the best compression algorithm made so far?
    By toaster in forum A Brief History of
    Replies: 13
    Last Post: 05-15-2002, 03:20 PM

Tags for this Thread