printHexidecimal function

Kinda lost on this, I know how to do it in C, also I could probably do it in C++, but we have a requirement to use bit operations to do this. Any help is appreciated. Code is below. I just need to write the function without changing main.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <climits

using namespace std;

// printHexadecimal: Writes the hexadecimal representation of word to
// output stream os.

void printHexadecimal(int word, ostream& os);


// printLine: Prints a horizontal line of length hyphens to output stream os
void printLine(int length, ostream& os);

extern const int N = sizeof(int) * CHAR_BIT; // # of bits in an int


int main()
{
int num;

// Print heading
printLine(60, cout);
cout << setw(9) << "Decimal" << setw(24) << "Binary"
<< setw(27) << "Hexadecimal" << endl;
printLine(60, cout);

while (cin >> num)
{
cout << right << setw(11) << num << setw(3) << " "
<< bitset<N>(num) << setw(4) << " ";
printHexadecimal(num, cout);
cout << endl;
}
printLine(60, cout);

return EXIT_SUCCESS;
}

void printLine(int length, ostream& os)
{
char ch = os.fill();
os << setfill(’-’) << setw(length) << "-" << setfill(ch) << endl;
}
You're going to delete your post again, so I don't think I'm going to help you further.
I am not deleting anymore. Realize it was an abuse of the forum, apologies, mbozzi.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

std::ostream& print_hex( unsigned int n, std::ostream& stm = std::cout )
{
    if( n < 16 ) return stm << "0123456789abcdef"[n] ;

    else
    {
        print_hex( n/16, stm ) ;
        return print_hex( n%16, stm ) ;
    }
}

int main()
{
    for( int n : { 0, 0xff, 0x123ab, 0xfecab98, -1, std::numeric_limits<int>::max(), std::numeric_limits<int>::min() } )
    {
        std::cout << "decimal " << std::dec << n
                  << "   hex " << std::hex << n
                  << "   print_hex " ;
        print_hex(n) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/5ff7d2fa4ea3012c
Topic archived. No new replies allowed.