0

I was trying to make a binary to decimal converter in c. But the out put that now comes is a random number. Before I that tried, it used to work for binary which consists of all 1s. eg when I enter 111 it used to give me 7. But when I entered 10 or 011 or anything else it gives me some random number. Now it gives me like it's a memory location. Please help. I tried with a online compiler.

 #include <stdio.h>
 #include<math.h>

 int main() {

int i=1, b[100];
char ch;
int decimal=0;
printf("enter binary\n");
ch=getchar();
while(ch!='\n'){
    ch=ch-'0';
    b[i]=ch;
    i++;
    ch=getchar();
}
i=i-1;
while(i>=0){
    decimal=(b[i]*((int)pow(2,i))) + decimal;
    i--;
}
printf("%d",(int)decimal);
return 0;
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • 1
    `i=1` How about the 0 index? The current code reads but does not set `b[0]`. – kaylum Nov 27 '21 at 10:30
  • Note that [`getchar`](https://en.cppreference.com/w/c/io/getchar) returns an **`int`**, which is rather important if you want to compare the result to the `int` value `EOF` (which you really should do). – Some programmer dude Nov 27 '21 at 10:40
  • Also don't use the floating-point `pow` function for dealing with powers of two, use bit-shifting instead. – Some programmer dude Nov 27 '21 at 10:41
  • 1
    Oh, and are you really learning C++? There's nothing specific to C++ in the code you show, it could all be plain C. If you're programming in C, then why did you add the C++ tag? Proper tagging is important to get the right people to see your questions. – Some programmer dude Nov 27 '21 at 10:42
  • Lastly a couple of points about `printf("%d",(int)decimal);` That makes the program invalid and not a proper [mre]. A proper [mre] should replicate the problem you ask about and *only* the problem you ask about. Always make sure to build and test your [mre] before copy-pasting it into your question. The second note is that the `%d` format expects an `int` argument, the variable `decimal` is an `int`, so the cast isn't needed. Why did you add the cast? – Some programmer dude Nov 27 '21 at 10:45

2 Answers2

1

As comments pointed out probably initializing i as 0 would reduce complexity of working with i as index. Therefore there won't be a free b[0] in array. It would also help to produce powers of 2 by a for loop , most of the times using pow function may lead to some unwanted casting behavior especially when double type output is not wanted.

Try this method of summation :

for (int j=0 ; j<i ; j++){
    decimal *=2 ;
    decimal += b[j] ;
}

Also for getting binary number it would work out to ask user the length of number and then using scanf("%s",b) to avoid character by character input.

0

The comments above suggest initializing i to 0 instead of 1. You definitely need to do that.

But you can also avoid the historical inaccuracies of the pow function by not using it and iterating over your array in the other direction. It enables you to stay in the integer space and avoid having to cast. Details here.

Instead of this:

while(i>=0){
    decimal=(b[i]*((int)pow(2,i))) + decimal;
    i--;
}

This:

for (int j = 0; j < i; j++) {
    decimal = decimal * 2 + b[j];
}
selbie
  • 100,020
  • 15
  • 103
  • 173