Converting decimal numbers to binary

Mar 23, 2015 at 5:39am
Hello everybody. I've written a program which can convert decimal numbers to binary numbers. But I was wondering whether it's possible to modify this code to accept decimal values as well, like 100.14, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int main ()
{
         int n,r,i=0,flag=0;
         int ch[16];
         cout<<"Enter the value of n as a decimal number : ";
         cin>>n;
         while (n!=0)
         {
               r=n%2;
               n=n/2;
               ch[i]=r;
               i++;
               flag++;
         }
         cout<<"The number is : ";
         for (i=flag-1;i>=0;i--)
               cout<<ch[i]<<" ";
}

Is this code any good or are there any errors or loopholes ?
Last edited on Mar 23, 2015 at 5:46am
Mar 23, 2015 at 9:17am
closed account (SECMoG1T)
possible to modify this code to accept decimal values as well, like 100.14
Well if you know something about templates and typeinfo inquiry then that's possible

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
 #include <string>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <type_traits>


 std::string fractional(double a)
{
  std::string data(".");
  unsigned int ival=a,count=0;
  double dval=a-ival;//get the fractional prt

  while(count<6)///just 6 bin digits are enough
  {
      dval*=2; int temp=dval;
      data+=std::to_string(temp);
      if(dval>1)
        dval-=temp;
      ++count;
  }

  return data;
}

template<typename T>
std::string dec_to_bin(T val)
{
    if(!std::is_arithmetic<T>::value)
        throw std::logic_error("T must be an arithmetic type\n");

    unsigned int dec=val;
    std::string temp;
    while(dec)
    {
        temp+=std::to_string(dec%2);
        dec/=2;
    }

    std::reverse(temp.begin(),temp.end());

    if(std::is_floating_point<T>::value)
    {
       temp+=fractional(val);
    }

    return temp;
}


int main()
{
    std::cout<<dec_to_bin(.9)<<std::endl;
    std::cout<<dec_to_bin(98)<<std::endl;
    std::cout<<dec_to_bin(456.987);
}


.111001 
1100010 
111001000.111111 
Last edited on Mar 23, 2015 at 9:41am
Mar 23, 2015 at 12:43pm
You don't need to increment i at line 14 since you'll reset the value at line 18.

Stylistically, it would be better to put i completely within the for loop at line 18. Also, I urge you to always use braces, even when they are redundant. Do this because otherwise, at 2AM you'll decide that you need 2 statements inside the for loop (or whatever doesn't have the braces) and you'll add it in. In other words, you'll go from this:
1
2
for (int i=0; i<count; ++i)
    do_something;
to this:
1
2
3
for (int i=0; i<count; ++i)
    do_something;
    and_do_something_else

But without the braces, and_do_something_else is actually outside of the loop. It will take you 4 hours to find this bug.... :)

Topic archived. No new replies allowed.