You forgot to describe the error. Presumably it's something like
‘bitchar’ was not declared in this scope
because you didn't declare the function before you called it in main
. Either move the definition of bitchar
before main
, or add a declaration before or inside main
:
std::string bitchar(char c);
Then you'll probably get something like:
invalid conversion from ‘const char*’ to ‘char’
because you're trying to assign a string literal "J"
to a character variable. Use a character literal 'J'
(with single quotes) instead.
Then you'll find you're not getting any output. That's because while (!thisdivisor)
loops as long as the value is zero; so it won't loop at all if you give it a non-zero value. You want while (thisdivisor)
(or while (thisdiviser != 0)
if you want to be more explicit), to loop while it's not zero.
Then you'll find that the bits are inverted; you want '0' if the modulo result is zero, while your test gives '0' if it is not zero:
s += thisdivisor % 2 ? '1' : '0';
or
s += (thisdivisor % 2 == 0) ? '0' : '1';
Finally, you might want to reverse the string (or build it by prepending rather than appending) to get the more conventional most-significant-bit-first ordering.