Binary to decimal

hi
I tried to write the program of binary to decimal
but what's wrong here?
there is no output :(
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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  int binary;
  int rem;
  int decimal = 0;
  int power = 0;
  int count = 0;

  cout << "Enter a binary : ";
  cin >> binary;

  while (count <= power)
    {
      binary /= 10;
      rem = binary % 10;
      decimal = rem * pow(2,power) + decimal;
      power++;
      count++;
    }

  cout << decimal << endl;
1
2
3
4
while (count <= power)
...
power++;
count++;


infinite loop?
mmmmm
I will try to fix this
thank you :)
1
2
 binary /= 10;
 rem = binary % 10;

logic seems wrong trace it out.
How the input will be ? Is it in 1010110 format or any number?
I think the logic seems to be wrong.
Hi first you have to specify if the binary you input has the MSB at the rightmost or leftmost bit. Then you must think on how to store the binary number. So you just input 0101011 and will use only the converted version or you will actually have binary data coming from another function. Anyways the using "int binary" is wrong. Just take into account a char array or a string would be better in case you will only input by hand.
Topic archived. No new replies allowed.