Converting to binary errors

Hi, I'm new to programming and I need to write a program that will convert decimals to binary and the other way around, I've decided to start with converting decimals to binary. Here is my code, I keep getting the error invalid conversion from int to int*, anyone know why?
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
#include<iostream>
#include<cmath>
using namespace std;
double decimalToBinary(double a,int b[]);
int main()
{
    double decimal;
    int binary_number;
    cout<<"Please enter a positive integer: ";
    cin>>decimal;
    decimalToBinary(decimal,binary_number);


}
double decimalToBinary(double a, int b[])
{
    int count=0;
    for (double i=10; i<=0; i--)
    {
        if(a>pow(2,i))
        {
            b[count]=1;
            a=a-pow(2,i);
            count++;
        }
        else
        {
            b[count]=0;
            count++;
        }
    }
    cout<<"The integer "<<a<<" in binary form is: "<<b<<endl;
}
binary_number is an int, and you pass it where an int[] is required. In function declarations int b[] is equivalent to int* b.

As for conversion, the algorithm is to divide the number by the new base and print the remainders in reverse order.
Thanks man, this was a great help. I keep getting 00000000 as my answer for some reason...I've looked over the code many times to no avail -.-
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
#include<iostream>
using namespace std;
int decimalToBinary(int a,int b[]);
int main()
{
    int decimal;
    int binary_number[10];
    cout<<"Please enter a positive integer: ";
    cin>>decimal;
    decimalToBinary(decimal,binary_number);


}
int decimalToBinary(int a, int b[])
{
    int count=0;
    int result;
    for (int i=0; i<=10; i++)
    {
        a=a%2;
        b[i]=a;
        count++;
    }
    
    cout<<"The integer "<<a<<" in binary form is: ";
    
    for (int j=count-1; j>=0; j--)
    {
        cout<<b[j];
        
    }

}

a=a%2; Let's say your 'a' was 100. 100%2 = 0 since 100 is even, so now your 'a' is 0 and it will remain so. Though if you had 103 you should get 1111111111..

What the code should be is
1
2
3
//in a loop
nth digit = a % 2;
a /= 2;
Ah yes, I see my error now...I fixed it, it now works correctly, except there are extra 0's because the array size is 10.

"Enter integer: 156"
"The integer in binary form is: 00010011100" The answer should be 10011100.

I tried to get it to stop executing a/=2 when a becomes 0 by doing this:
1
2
3
4
5
6
7
8
9
10
    int count=0;
    for (int i=0; i<=10; i++)
    {
        b[i]=a%2;
        a/=2;
        count++;
        if (a=0)
        break;

    }

Now I'm getting 0's again -.-
Very frustrating...



line 7 = is assignment, == is comparison.
You're awesome, thanks :)
Topic archived. No new replies allowed.