This program writes the digits to cout
.
A more general solution is to write the conversions
as functions which put their output into a character array as
c-strings. Write these conversions as functions with the following
prototypes:
void intToBinary(char result[], int n);
void intToHex(char result[], int n);
// A call might look like this:
char binary[50]; // big enough to hold an int with spaces.
. . .
while (cin >> n) {
intToBinary(binary, n); // produce binary character string.
cout << binary << " has " < strlen(binary)
<< " digits." << endl;
}
There is a big difference between the hex conversion and the binary
conversion. The hex conversion writes a char to cout while the
binary conversion writes an int. It's necessary to put a char in the
output array of the function of course.