Conversion of a decimal number(<2^12) to binary.

Can someone please explain why the code isn't compiling.
(especially in the last 3 lines of the function definition)

I've used a recursion function in the 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
  
#include <iostream>
#include <cmath>
using namespace std;
  
  
void f(int n)
{
    int arr[12],k;
    
    for(int l=0; l<12; l++)
    {
        arr[l]=0;
    }
    
    bool tf=true;
    for(int i=0; i<11; i++)
    {
        if(pow(2.0, i)<=n) {k=i;tf=false;}
        if(tf) break;
    }
    
    arr[k]=1;
    
    if(fmod(n,pow(2.0,k))>1) cout<< f(fmod(n,pow(2.0,k)));
    if(fmod(n,pow(2.0,k))==1) {arr[0]=1; cout<< arr;}
    if(fmod(n,pow(2.0,k))==1) {arr[0]=0; cout<< arr;}
}

int main()
{
    int n;
    cin>>n;
    cout<<f(n);
    
    system("pause");
    return 0;
}
    
Last edited on
In function 'void f(int)': 25:57: error: no match for 'operator<<' in 'std::cout << f((int)fmod()(n, pow()(2.0e+0, k)))'
compiler wrote:
In function 'void f(int)', line 25, column 57: There is no way to resolve this expression `'std::cout << f((int)fmod()(n, pow()(2.0e+0, k)))'`. Aka I don't know what to print as a return value for a function returning void. It is not defined in my knowledge base.


The error message is quite explanatory. Also the only "return" portion of the code I see is the return statement in the main function, and the problem is definitely not there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

std::string tobin(double value) {
    unsigned long long arr = *reinterpret_cast<unsigned long long *>(&value);
    std::string out("");
    for (int t = 63; t >= 0; t--) {
        out += std::to_string((arr >> t) & 1);
        arr &= ((1ULL << t) - 1);
    }
    return out;
}

int main() {
    std::cout << tobin(123123.232) << std::endl;
}
Last edited on
Topic archived. No new replies allowed.