Link to home
Start Free TrialLog in
Avatar of jnowlin
jnowlin

asked on

Output a Number, in Decimal, Hex and Octal

Given the following code segment:

#include <iostream.h>

int main()
{
      int num = 0;
      cout << "Enter an integer: " << endl;
      cin >> num;

      cout << "Format: decimal, octal & hexadecimal." << endl;
      //cout.setf(ios::dec, ios::basefield);
      //cout << num;                           ??????
      //cout.setf(ios::oct, ios::basefield);
      //cout << num;
      cout.setf(ios::hex, ios::basefield);
      cout << num;

  //  cout << num;

return(0);
}

I need to output the number entered in decimal, hexidecimal and octal
format, all at once.

Why can't I string together the appropriate format specifiers, as I originally
tried in the above code?
ASKER CERTIFIED SOLUTION
Avatar of kellyjj
kellyjj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jnowlin, kellyjj's answer is correct but there is one more thing that he forgot to mention.

the << operator returns a reference to the stream, allowing concatenation.  Thus:

    cout << hex << num << ' ' << dec << num << ' ' << oct << num << endl;