Programming in C++ Another Way to Read char Data The get() function can be used to read a single character It obtains the very next character from the input stream without skipping any leading whitespace characters 16
16 The get( ) function can be used to read a single character. It obtains the very next character from the input stream without skipping any leading whitespace characters. Another Way to Read char Data
Programming in C++ An Example Using cin. get() At key board you ty pe: A[spacelB[space]C[Enterl char first char middle char last first middle last cin, get( first), cin.get( middle ) cin.get( last); first middle last NOTE: The file reading marker is left pointing to the space after the 'B in the input stream
17 char first ; char middle ; char last ; cin.get ( first ) ; cin.get ( middle ) ; cin.get ( last ) ; NOTE: The file reading marker is left pointing to the space after the ‘B’ in the input stream. first middle last An Example Using cin.get( ) At keyboard you type: A[space]B[space]C[Enter] first middle last ‘A’ ‘ ’ ‘B’
Programming in C++ Use function ignore() to skip characters The ignore() function is used to skip (read and discard) characters in the input stream. The cal cin.ignore( howMany, whatcha ), will skip over up to howMany characters or unti whatcha has been read. whichever comes first
18 Use function ignore( ) to skip characters The ignore( ) function is used to skip (read and discard) characters in the input stream. The call cin.ignore ( howMany, whatChar ) ; will skip over up to howMany characters or until whatChar has been read, whichever comes first
Programming in C++ An Example Using cin. ignore( NOTE shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int a 3 957341235n int b; 12896n int C: a b cin > a > b. 95734 957341235n 12896n a cin. ignore(100, In):957 34 957341235n 12896n a b cIn >> C: 95734 128957341235n 12896ln a b 19
19 An Example Using cin.ignore( ) a b c a b c a b c a b c 957 34 957 34 128 957 34 NOTE: shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int a ; 957 34 1235\n int b ; 128 96\n int c ; cin >> a >> b ; 957 34 1235\n 128 96\n cin.ignore(100, ‘\n’) ; 957 34 1235\n 128 96\n cin >> c ; 957 34 1235\n 128 96\n
Programming in C++ Another Example Using cin. ignore() NOTE: shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int i A22B16c19n char ch ch cin > ch A22B16c19n ch cin.ignore(100,B), A22B16c19n ch cin > I, 16 A A22B16c19n ch 20
20 Another Example Using cin.ignore( ) i ch 957 34 957 34 957 34 i ch i ch i ch 16 ‘A’ ‘A’ ‘A’ NOTE: shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int i ; A 22 B 16 C 19\n char ch ; cin >> ch ; A 22 B 16 C 19\n cin.ignore(100, ‘B’) ; A 22 B 16 C 19\n cin >> i ; A 22 B 16 C 19\n