Programming in C++ Extraction operator >>(cont) When using the extraction operator(>>)to read input characters into a string variable the >> operator skips any leading whitespace characters such as blanks and newlines it then reads successive characters into the string, and stops at the first trailing whitespace character(which is not consumed, but remains waiting in the input stream)
11 Extraction operator >>(cont.) When using the extraction operator ( >> ) to read input characters into a string variable: ❖ the >> operator skips any leading whitespace characters such as blanks and newlines ❖it then reads successive characters into the string, and stops at the first trailing whitespace character (which is not consumed, but remains waiting in the input stream)
Programming in C++ The Reading Marker and the Newline Character reading marker o newline(end-of-line The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or " In In a program. 12
12 The Reading Marker and the Newline Character ❖ reading marker ❖ newline (end-of-line ) The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program
Programming in C++ Example 1: At key board you type A[space]B[space]C[] char first char middle char last first middle last cin > first B cin > middle cin > last first middle last NOTE: A file reading marker is left pointing to the newline character after the ' in the input stream. 13
13 char first ; char middle ; char last ; cin >> first ; cin >> middle ; cin >> last ; NOTE: A file reading marker is left pointing to the newline character after the ‘C’ in the input stream. first middle last Example 1:At keyboard you type: A[space]B[space]C[Enter] first middle last ‘A’ ‘B’ ‘C’
Programming in C++ Example 2: At keyboard you type. [space]25[space]J[space]2 [Enter] int age char initial float bill age initial bil cin > age i 25 2.0 cin > initial cin > bill age initial bill NOTE: A file reading marker is left pointing to the newline character after the 2 in the input stream 14
14 Example 2:At keyboard you type: [space]25[space]J[space]2[Enter] int age ; char initial ; float bill ; cin >> age ; cin >> initial ; cin >> bill ; NOTE: A file reading marker is left pointing to the newline character after the 2 in the input stream. age initial bill age initial bill 25 ‘J’ 2.0
Programming in C++ Example 3 NOTE shows the location of the file reading marker STATEMENTS CONTENTS MARKER POSITION int 2 5 AIn char ch 16.9n float x ch X cin > I 25 25 AIn 16.9n ch X cin > ch 25 25 AIn 16.9n ch X cin > X 25 16925An 16.9n ch X 15
15 STATEMENTS CONTENTS MARKER POSITION int i ; 25 A\n char ch ; 16.9\n float x ; cin >> i ; 25 A\n 16.9\n cin >> ch ; 25 A\n 16.9\n cin >> x ; 25 A\n 16.9\n Example 3 i ch x 25 25 ‘A’ i ch x i ch x i ch x 25 ‘A’ 16.9 NOTE: shows the location of the file reading marker