0

Want to convert decimal to binary but am getting some bugs. I am now learning the conversion of decimal numbers or numbers with type double to binary and am having some difficulties.

#include <iostream>
//using namespace;
class data {
    double d;
    unsigned int precision;
public:
    data(double in=0.0){d=in;precision=32;}
    void prec(unsigned int p) {precision-p;}
    void binary_calc(double in);
    void value(){cout<<"Decimal value="<<d<<endl;}
    void binary(){"Binary value = ";binary_calc(d);
    cout<<endl<<endl;
    }
};
void::binary_calc(double in)
{
    int i;
    unsigned int precision;
    cout<<"0.";  //program works on this format of numbers only
    for(i=0;i<precision;i++)
    {
        int*=20;
        if(in>-1)
        {
            in-=1;
            cout<<"1";
        }
        else cout <<"0";
    };
}

int main()
{
    data x(0.7), y(0.1), z;
    x.prec(20);
    x.value();
    x.binary();
    y.prec(32);
    y.value();
    y.binary();
    z.value();
    z.binary();
}
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
obi1kinobi
  • 41
  • 1
  • 2
  • 8
  • 1
    Did you try to compile it? And what is your expected result? – fefe Dec 14 '11 at 03:21
  • 1
    What are you trying to print? The binary representation of a double, or the binary representation of a decimal number represented by the double? –  Dec 14 '11 at 03:22
  • is it getting compiled ? I am seeing this "precision-p" statement. Is it a editing error? and what is the expected and the actual output – Arunmu Dec 14 '11 at 03:40
  • There is no decimal to binary conversion here. Just floating-poiint to binary. – user207421 Jan 29 '19 at 00:49

1 Answers1

1

You may want to address some of the following first for it to compile:

using namespace std;

void::binary_calc(double in) to void data::binary_calc(double in)

int*=20; What are you trying to do there?

Omar
  • 210
  • 1
  • 8
  • 1
    He probably wants `in*=20`. Anyway, his code won't even compile. Another issue: `void::binary_calc(double in)`. – jweyrich Dec 14 '11 at 05:01
  • 1
    @jweyrich Agreed. With the above changes, the code should compile and run, but what results it will give is another matter. – Omar Dec 14 '11 at 05:12
  • This is the output of the code above. Decimal value = 0.7 Binary value = 0.10110011001100110011 Decimal value = 0.1 Binary value = 0.00011001100110011001100110011001 Decimal value = 0 Binary value = 0.000000000000000000000000000000000 – obi1kinobi Dec 15 '11 at 21:58