After inputing codeword in sender's side have it coded with crc generator poly , on the reveiver side it has to be decoded and checked for error , and the error can be input manually. This is the sender side (without coding the input) It worked fine:
#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main ()
{
string msg,crc,encoded="";
cout<<"Enter the message=";
getline(cin,msg);
cout<<"Enter the crc generator polynomial=";
getline(cin,crc);
int m=msg.length(), n=crc.length();
encoded+=msg;
for(int i=1 ; i<=n-1; i++)
encoded+='0';
for(int i=0;i <=encoded.length()-n; )
{
for(int j=0;j<n; j++)
encoded[i+j]= encoded[i+j]==crc[j]? '0' :'1';
for(; i<encoded.length() && encoded[i]!='1'; i++);
}
cout<<msg+encoded.substr(encoded.length()-n+1);
return 0;
}
sender with coding input(from text to binary):
#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
string msg,crc,encoded="";
cout<<"Enter the message=";
getline(cin,msg);
cout<<"Enter the crc generator polynomial=";
getline(cin,crc);
int m=msg.length(), n=crc.length();
encoded+=msg;
for (std::size_t i = 0; i < msg.length(); ++i)
{
for(int i=1 ; i<=n-1; i++)
encoded+='0';
for(int i=0;i <=encoded.length()-n; )
{
for(int j=0;j<n; j++)
encoded[i+j]= encoded[i+j]==crc[j]? '0' :'1';
for(; i<encoded.length() && encoded[i]!='1'; i++);
}
cout << bitset<8>(msg.c_str()[i]) << endl;
cout<<msg+encoded.substr(encoded.length()-n+1);
return 0;
}
}
Receiver side, i really tried a lot and i didn't know how to decode it and still check for error
#include <iostream>
using namespace std;
int main ()
{
string crc,encoded;
cout<<"Enter the message=";
getline(cin,encoded);
cout<<"Enter the crc generator polynomial=";
getline(cin,crc);
for(int i=0;i <=encoded.length()-crc.length();){
for(int j=0;j<crc.length();j++)
encoded[i+j]= encoded[i+j]==crc[j]? '0':'1';
for(; i<encoded.length() && encoded[i]!='1'; i++);
}
for(char i: encoded.substr(encoded.length()-crc.length()+1))
if(i!='0'){
cout<<"Error in comunication...";
return 0;
}
cout<<"No error";
}