Von Neuman Loves Binary CHALLENGE

Link to the Problem: https://online.codingblocks.com/player/18850/content/4214/4692

I am getting wrong answer in one of the test cases with the below program.

#include
using namespace std;

int main() {
int N;
int a[16];
cin>>N;
char ch = cin.get();
for(int i = 0; i < N; i++){
int j=-1;
ch = cin.get();
while(ch != ‘\n’ && ch!=EOF)
{
j++;
a[j]=ch-‘0’;
ch = cin.get();
//cout<<a[j]<<" “;
}
// cout<<a<<” ";
int k = 1;
int sum = 0;
while(j>=0){
if(a[j]!=0)
sum = sum + a[j]k;
k = k
2;
j–;
}
cout<<sum<<endl;
}

return 0;

}

@guptamegha98 copy your code on ide.so we can help you asap

https://ide.codingblocks.com/s/71736

Hi Megha, while your algorithm to convert the binary number to decimal is absolutely correct, you are not taking/processing the input correctly. Pls try and take input in some other way/form.

You can try taking each number input as a string, or as a number and using x%10, x/=10 keep on finding it’s last digit and multiplying with powers of 2.

In you method the while loop in line 12 is a potential cause of error.

I took each binary number as a number( long long int) and now both the test cases got cleared. But i still couldn’t understand the error in line 12 of my previous program.

the error could have been the termination of input file and your condition to terminate input were not matching. But this should never be an issue for a programmer, that how the input file is saved. Only thing important is the input format. here u were using cin.get() which at some point in testcase 1 got mismatched.