Binary to Dec program

Hi guys, I wanna convert binary numbers to decimal without using #include<cmath>. The code I have now can convert most of the "1" and "0" into correct decimal, however I try to input numbers like 12111211 it suppose to be invalid but it show some weird numbers. I would like to perform a number test before the convert operation start, but I had no idea how to do it. Here is my code

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
int main()
{
    int num;
    cout << "8 bit Binary Numbers = " ;
    cin >> num;
    cout << num << endl;

    bool valid = true;
    int sum = 0;
    int c = 0;
    int x = num;
    int p = 1;
    int q = 0;
    int r = 0;
    int y = num;

    valid = ((num%10 == 1 || num%10 == 0 )&& num <= 11111111 );
    if((num%10 == 1 || num%10 == 0 )&& num <= 11111111 )
     {
         while( c <= 7)
          {
            x = y%10;
            y = y/10;

                if (x == 1 || x == 0)
                  {
                     r = x;

                         if(c == 0)
                           {
                             p = 1;
                           }
                         else
                           {
                             p = p*2;
                           }
                     q = r*p;
                     c++;
                     sum = sum + q;
                     q = 0;
                     r = 0;
                   }
          }
    }
    else
    {
    cout << "Entered numbers aren't binary numbers!" << endl;
    }

    if(valid)
    {
    cout << "Decimal Numbers=" << sum ;
    }

}


Anyone can help on these?
Firstly i think num should be declared as std::string instead of int for generality. int cannot take large numbers as input. If you decide to increase your number of bits, then int will fail.

Also, your check for a valid number is incorrect. You are checking only for the 1st digit to be a binary number and that the number should be less than 11111111. This means that a number like 561 would be wrongly treated as a valid binary number.

Modify your code to something like the following. (haven't tested it, but it should give you an idea)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string num;
    cout << "8 bit Binary Numbers = " ;
    cin >> num;
    cout << num << endl;

    bool valid = true;
    int sum = 0;
    int c = 7;
    int x = 0;
    int p = 1;
    int q = 0;
    int r = 0;
    //string y = num;
//    valid = ((num%10 == 1 || num%10 == 0 )&& num <= 11111111 );
    if (num.size() != 8)
      valid = false;
    else {
      for (int i=0; i<8; i++)
        if (num[i] != '0' && num[i] !='1') {
          valid = false;
           break;
        }
    }
    if (valid)
     {
         while( c <= 7)
          {
            x = num[c] - '0';
//            y = y/10;

                if (x == 1 || x == 0)
                  {
                     r = x;

                         if(c == 7)
                           {
                             p = 1;
                           }
                         else
                           {
                             p = p*2;
                           }
                     q = r*p;
                     c--;
                     sum = sum + q;
                     q = 0;
                     r = 0;
                   }
          }
    }
    else
    {
    cout << "Entered numbers aren't binary numbers!" << endl;
    }

    if(valid)
    {
    cout << "Decimal Numbers=" << sum ;
    }

}


Last edited on
The original question actually start from the line bool valid = true, so I could not include string into it and do this without string method. I get the idea how to test the number, but I failed to test every number which bugged me for few days just for the exact condition.

Thanks for the idea, I will try to modify right away.
The problem I see with getting the input as a numeric type rather than a character string, is that this code is doing a conversion of its own:
1
2
3
    int num;
    cout << "8 bit Binary Numbers = " ;
    cin >> num;

At the prompt, the user types a string of characters. the cin >> num statement takes those characters, treat them as decimal digits and translates and stores the resulting input as a binary value in the variable num.

The integer type uses an internal binary representation in order to store the value. That forces the code to to an extra step of converting the binary value into decimal and makes the code more complicated than necessary:
1
2
            x = y%10; // get decimal digit from binary value
            y = y/10;

It also limits the range of values which the user is able to input, as the number of decimal digits is constrained.
Topic archived. No new replies allowed.