Input:
101101110
1101
Expected Ouput:
000000000011
My output:
It just keeps on taking the input.and not showing any output.
Please help me . what is wrong with my code. Any help would be aprreciated.I have given the names of the variables such that its easy to understand.This code is only for the senders side.
#include <iostream>
using namespace std;
int main()
{
string input;
string polynomial;
string encoded="";
cin>>input;
cin>>polynomial;
int input_len=input.length();
int poly_len=polynomial.length();
encoded=encoded input;
for(int i=1;i<=poly_len-1;i ){
encoded=encoded '0';
}
for(int i=0;i<=encoded.length()-poly_len;){
for(int j=0;j<poly_len;j ){
if(encoded[i j]==polynomial[j]){
encoded[i j]=='0';
}
else{
encoded[i j]=='1';
}
}
while(i<encoded.length() && encoded[i]!='1'){
i ;
}
}
cout<<encoded;
return 0;
}
CodePudding user response:
Look at these lines properly:
if (encoded[i j] == polynomial[j]) {
encoded[i j] == '0'; // Line 1
}
else {
encoded[i j] == '1'; // Line 2
}
See? You are using ==
while you should be using =
. ==
is a comparison operator which returns a bool
(true/false). It does not assign values. So to fix your problem, replace the above lines with:
if (encoded[i j] == polynomial[j]) {
encoded[i j] = '0'; // Replaced == with =
}
else {
encoded[i j] = '1'; // Replaced == with =
}
This should fix your problem.
CodePudding user response:
My tip for you is to get familiar with debugging. Try adding some breakpoints in your code so you can actually see what is happening behind. After checking your code it seems that this line is giving an infinite loop for(int i=0;i<=encoded.length()-poly_len;)
. The condition won't be true at any point after entering the input you gave us.