SLIDE 6 2013-‑09-‑10 ¡ 6 ¡
Derived ¡type: ¡istream ¡
- #include <iostream>
- type ¡of ¡the ¡predefined ¡object ¡”cin” ¡
- ”cin” ¡is ¡used ¡to ¡read ¡from ¡keyboard ¡
- associated ¡operaNons ¡
>> // formatted input void ignore(int count, char stop) int get()
– getline(istream in, string line, char stop)
int age; cin >> age; cin.ignore(10, ’\n’); string name; getline(cin, name, ’\n’);
Derived ¡type: ¡ostream ¡
- #include <iostream>
- type ¡of ¡the ¡predefined ¡objects ¡”cout” ¡and ¡”cerr” ¡
- ”cout” ¡is ¡used ¡to ¡write ¡normal ¡output ¡to ¡screen ¡
- ”cerr” ¡is ¡used ¡to ¡write ¡error ¡output ¡to ¡screen ¡
- associated ¡operaNons ¡
<< // formatted output
int age{7}; cout << age << ” years old\n”;
Forma]ed ¡input ¡
– Keyboard ¡load ¡”train” ¡with ¡characters ¡ – Loading ¡”enter” ¡key ¡give ¡signal ¡for ¡train ¡departure ¡ – Train ¡stop ¡at ¡programs ¡staNon ¡ – Program ¡unload ¡every ¡character, ¡one ¡at ¡a ¡Nme ¡
– Discards ¡blank ¡characters ¡ – Tries ¡to ¡convert ¡nonblank ¡characters ¡to ¡requested ¡ type ¡(depend ¡on ¡desNnaNon ¡variable!) ¡ – Leaves ¡remaining ¡characters ¡on ¡train ¡
Forma]ed ¡output ¡
– Program ¡load ¡”train” ¡with ¡characters ¡ – Loading ¡special ¡”endl” ¡or ¡”flush” ¡give ¡signal ¡for ¡ train ¡departure ¡ – Train ¡stop ¡at ¡screen ¡ – Screen ¡unloads ¡and ¡display ¡characters ¡
– Converts ¡values ¡to ¡a ¡character ¡sequence ¡ – Applies ¡formavng ¡according ¡to ¡set ¡rules ¡
Manipulators ¡for ¡formavng ¡
- #include <iomanip>
- Manipulate ¡input ¡
– hex, ¡oct, ¡dec ¡ – ws ¡
– hex, ¡oct, ¡dec, ¡boolalpha, ¡noboolalpha ¡ – endl, ¡flush ¡ – setw(int ¡width) ¡ – leM, ¡right ¡ – sewill(char ¡symbol) ¡ – setprecision(int ¡digits) ¡ – fixed, ¡scienNfic ¡
Example ¡
- Write ¡a ¡program ¡to ¡calculate ¡how ¡many ¡hours ¡
- f ¡this ¡course ¡you ¡are ¡expected ¡to ¡work ¡on ¡
your ¡own ¡Nme ¡
– 4 ¡scheduled ¡sessions ¡each ¡week ¡on ¡average ¡ – 2 ¡hours ¡per ¡scheduled ¡session ¡ – 14 ¡weeks ¡of ¡scheduled ¡sessions ¡ – 6hp ¡for ¡enNre ¡course ¡ – 2p ¡for ¡each ¡3hp ¡ – 40hours ¡for ¡each ¡1p ¡