1. Learn to indent code. Pick a style and stick to it.
Code:
#include <iostream>
#include <string>
using namespace std;
string comparision(string hextext)
{
//string hextext = "";
int counter = 0;
int length = hextext.size();
std::string twochars = "";
std::string secondtwochars = "";
//trace() << "Input : " << hextext;
string runlengthengthcoding = "";
/*for(int w=0;w<52;w++){
if(temphexholder[0] == databytes[w]){
counter++;
}
if(temphexholder[0] != databytes[w]){
runlengthengthcoding += databytes[w] + counter;
counter = 0;
temphexholder[0] = databytes[w];
}
} */
for (int i = 0; i < length; i += 4) {
twochars = hextext.substr(i, 2);
for (int k = 2; k < length; k += 4) {
secondtwochars = hextext.substr(k, 2);
if (twochars == secondtwochars) {
counter++;
}
if (twochars != secondtwochars) {
string counters = to_string(counter);
runlengthengthcoding += twochars + counters;
counter = 0;
}
}
}
return runlengthengthcoding;
}
int main()
{
string t = "30304040";
cout << comparision(t);
}
See how much nicer this looks.
2. Even if it looks pretty in your IDE, do NOT mix spaces and tabs for indentation (use spaces only). Sooner or later, something other than your editor will render all the tabs as dog food.
3. Test before you post!.
Code:
foo.cpp: In function ‘int main()’:
foo.cpp:46:23: error: ‘comparison’ was not declared in this scope
cout << comparison(t);
This just makes it look like you typed it from memory.
4. Snipping out a couple of lines of includes does nothing but frustrate people who might otherwise bother to help. Your ideal should be that we should be able to copy/paste/compile/test with minimal hassle. If we have to start patching up your code just to make it work, your help rate will fall off a cliff.
5. Learn to use a debugger.
30 seconds with a debugger would have shown you just what the problem was.
Code:
$ g++ -g -std=c++11 foo.cpp
$ ./a.out
30-1111830727400$
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b 33
Breakpoint 1 at 0x4018fe: file foo.cpp, line 33.
(gdb) run
Starting program: a.out
Breakpoint 1, comparision (hextext="30304040") at foo.cpp:33
33 string counters = to_string(counter);
(gdb) print counter
$1 = -8711
The reason - uninitialised variables.
Now you know you can breakpoint, step and print, you should try and debug the next problem yourself.