obi1kinobi
obi1kinobi

Reputation: 41

Convert from decimal to Binary

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();
}

Upvotes: 0

Views: 1029

Answers (1)

Omar
Omar

Reputation: 220

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?

Upvotes: 1

Related Questions