Sams Teach Yourself C++ in 21 Days

(singke) #1
Working with Streams 619

17


11:
12: cout.setf(ios::showbase);
13: cout << “The number is “ << hex << number << endl;
14:
15: cout << “The number is “ ;
16: cout.width(10);
17: cout << hex << number << endl;
18:
19: cout << “The number is “ ;
20: cout.width(10);
21: cout.setf(ios::left);
22: cout << hex << number << endl;
23:
24: cout << “The number is “ ;
25: cout.width(10);
26: cout.setf(ios::internal);
27: cout << hex << number << endl;
28:
29: cout << “The number is “ << setw(10) << hex << number << endl;
30: return 0;
31: }

The number is 185
The number is b9
The number is 0xb9
The number is 0xb9
The number is 0xb9
The number is 0x b9
The number is:0x b9
On line 7, the constant int numberis initialized to the value 185. This is dis-
played normally on line 8.
The value is displayed again on line 10, but this time the manipulator hex is concate-
nated, causing the value to be displayed in hexadecimal as b9.

OUTPUT


LISTING17.4 continued


ANALYSIS

The value bin hexadecimal represents 11. Eleven times 16 equals 176; add
the 9 for a total of 185.

NOTE


On line 12, the flag showbaseis set. This causes the prefix 0xto be added to all hexadec-
imal numbers, as reflected in the output.
Free download pdf