Output for different data types
Jul 27, 2011 at 10:30pm UTC
In this program I am trying to get a number the user inputs to display in hex and decimal for different data types.The comment portions are the desired output.
I have been using
#include <iomanip>
so I can change the number to "hex" and "dec" but everything that I've hard-coded in, except for the bool, char, etc., isn't giving me the correct output. For example on line 10 I've tried
<< dec << number
and
<< hex << number
But other ones like the unsigned short on line 24 work just fine.
Is there a reason why it is working with some data types and not with others?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
void displayData(int number)
{
// bool 1 1 1 N/A
cout << "bool" << setw(18) << sizeof (bool ) << setw(24)
<< bool (number) << setw(22) << bool (number) << setw(9)
<< "N/A" << endl;
// char 1 -1 ffffffff T
cout << "char" << setw(18) << sizeof (char ) << setw(24)
<< "-1" << setw(22) << "ffffffff"
<< setw(7) << "T" << endl;
// unsigned char 1 255 ff T
cout << "unsigned char" << setw(9) << sizeof (char )
<< setw(24) << "255" << setw(22) << "ff"
<< setw(7) << "T" << endl;
// short 2 -1 ffff T
cout << "short" << setw(17) << sizeof (short ) << setw(24)
<< "-1" << setw(22) << hex << number
<< setw(7) << "T" << endl;
// unsigned short 2 65535 ffff F
cout << "unsigned short" << setw(8) << sizeof (short )
<< setw(24) << dec << number << setw(22) << hex
<< number << setw(7) << "F" << endl;
// int 4 65535 ffff F
cout << "int" << setw(19) << sizeof (int ) << setw(24)
<< dec << number << setw(22) << hex << number
<< setw(7) << "F" << endl;
// unsigned int 4 65535 ffff F
cout << "unsigned int" << setw(10) << sizeof (int )
<< setw(24) << dec << number << setw(22)
<< hex << number << setw(7) << "F" << endl;
// long 4 65535 ffff F
cout << "long" << setw(18) << sizeof (long ) << setw(24)
<< dec << number << setw(22) << hex << number
<< setw(7) << "F" << endl;
// unsigned long 4 65535 ffff F
cout << "unsigned long" << setw(9) << sizeof (long )
<< setw(24) << dec << number << setw(22)
<< hex << number << setw(7) << "F" << endl;
// long long 8 65535 ffff F
cout << "long long" << setw(13) << sizeof (long ) + sizeof (long )
<< setw(24) << dec << number << setw(22) << hex << number
<< setw(7) << "F" << endl;
// unsigned long long 8 65535 ffff F
cout << "unsigned long long" << setw(4) << sizeof (long ) + sizeof (long )
<< setw(24) << dec <<number << setw(22) << hex
<< number << setw(7) << "F" << endl;
// float 4 65535 6.55350e+04 F
cout << "float" << setw(17) << sizeof (float ) << setw(24)
<< dec << number << setw(22) << "6.55350e+04"
<< setw(7) << "F" << endl;
// double 8 65535 6.55350e+04 F
cout << "double" << setw(16) << sizeof (double ) << setw(24)
<< dec << number << setw(22) << "6.55350e+04"
/*hex << number*/
<< setw(7) << "F" << endl;
// long double 12 65535 6.55350e+04 F
cout << "long double" << setw(11) << (sizeof (long ) + sizeof (double ))
<< setw(24) << dec << number << setw(22) << "6.55350e+04"
<< setw(7) << "F" << endl;
}
Jul 27, 2011 at 11:38pm UTC
Because the std::hex manipulator only works with integral types. Note that char is not considered an integral type, but unsigned char is.
Topic archived. No new replies allowed.