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?
#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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the << operator returns a reference to the stream, allowing concatenation. Thus:
cout << hex << num << ' ' << dec << num << ' ' << oct << num << endl;