Ouch. What a mess :) You need to address the C++ errors, and logical errors as well.
Works well to line 11
Add ; to lines 66 and 67
Your line 15: how do you know that the character last seen was 0? You should initialize that value to the first value in the data_stream
Line 18: "for( bit<=data_stream); " is not a valid C++ statement. See
http://www.cplusplus.com/doc/tutorial/control/ the for loop.
for (initialization; condition; increase) statement; |
So you need a counter for position along the data string. Before the for loop declare
size_t position;
the initialization can be
position=0
, the condition is to be before the end of the string
position<data_stream.size()
and the increase is
position++
.
After the for(...) you need to put a statement. The syntax for that is either
for (initialization; condition; increase) statement;
or
1 2 3 4 5 6
|
for (initialization; condition; increase)
{
statement1;
statement2;
statement3;
}
|
Please note where you have semicolons.
Further errors: the assignment operator in C++ is "=", not ":="
"this" is a c++ reserved keyword. Do not use it until you are a little bit more advanced.
For the logic of your code, here is the first half (replace this between lines 18 and 38)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
// stuff a zero bit after four consecutive bits of the same value.
std::string stuffed_stream ;
int cnt = 0 ;
char bit_last_seen;
int stuff_bits_added = 0 ;
for( size_t p=0;p<data_stream.size();p++) //for each bit in the data_stream
{
bit=data_stream[p];
//1st step: copy the bit - you always need to do this step
stuffed_stream+=bit;
if(cnt==0) // this condition occurs either at the beginning, or after 4 occurences have been found
//set the bit_last_seen, increase the cnt to 1, then move to the next bit
{
bit_last_seen =bit;
cnt++;
}
else //do your processing
{
if( bit==bit_last_seen) //it is the same as the previous bit
{
cnt++;
}
else //different bit
{
bit_last_seen = bit ;
cnt = 1 ; // restart count at 1
}
if( cnt == 4 ) // there are four consecutive bits of the same value
{
stuffed_stream += '0' ; // stuff with a zero bit
cnt = 0 ; // and reset cnt to zero
++stuff_bits_added ; // increment the count of stuff bits added
}
}
}
|
i suggest you comment out the rest of your code, except the printing, run the first part, understand the explanations, then write the second part.