Please help me with this program..!!

i am writing a program that reads in a sequence of binary digits 1 or 0 and stores in STL container. The input terminates on any input other than 0 and 1. After finishing the read process applying bit stuffing to the container. In this case bit stuffing occurs after 4 consecutive bits of the same value. Also destuffing code to process the stuffed data and recreate the original data and verify the original data is recovered correctly.


I am not able to run this code. Can someone please help me with it.
i did run the program but the output obtained is specifically not correct.

here is the code i am trying with using string (as STL container) but i am unable to run it due to error.
can u please help me with this code.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
using namespace std;

int main()
{
    // read bits '0'/'1' into a string; terminate on any other char
    std::string data_stream ;
    char bit ;
    while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;

    // stuff a zero bit after four consecutive bits of the same value.
    std::string stuffed_stream ;
    int cnt = 0 ;
    char bit_last_seen = 0 ;
    int stuff_bits_added = 0 ;
    
    for( bit<=data_stream);           //for each bit in the data_stream
    {
        if( bit==bit_last_seen)       //it is the same as the previous bit
			cnt++;                      

        else // it is a different bit 
        {
            bit_last_seen := this bit ;
            cnt := 1 ; // restart count at 1
        }

        stuffed_stream += bit ; // add the bit 

        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
        }
    }

	std::string destuffed_stream ;
    int cnt = 0 ;
    char bit_last_seen = 0 ;
    int stuff_bits_added = 0 ;
    
    for( bit<=data_stream);           //for each bit in the data_stream
    {
        if( bit==bit_last_seen)       //it is the same as the previous bit
			cnt--;                      

        else // it is a different bit 
        {
            bit_last_seen := this bit ;
            cnt := 1 ; // restart count at 1
        }

        stuffed_stream -= bit ; // sub the bit 

        if( cnt == 4 ) // there are four consecutive bits of the same value
        {
            destuffed_stream += '0' ; // stuff with a zero bit
            cnt := 4 ; // and reset cnt to 4
            --stuff_bits_added ; // decrement the count of stuff bits added
        }
    }
    
    // print out the results  
    std::cout << "     data stream: " << data_stream << '\n'
    std::cout << "  stuffed stream: " << stuffed_stream << '\n'
    std::cout << "stuff bits added: " << stuff_bits_added << '\n' ;
	std::cout << "de-stuff bits added: " << destuffed_stream << '\n' ;
           
}
Last edited on
Please help..!!!!
A few things:

First, you should probably read this: http://www.cplusplus.com/forum/beginner/1/. The way you've presented your question strongly discourages people from responding.

Second, I cannot compile your code. My compiler's first error is:
18: error: no match foroperator<=’ in ‘bit <= data_stream’

Third, you might want to try http://www.cplusplus.com/forum/beginner/ for this one.
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.
Last edited on
Topic archived. No new replies allowed.