13.4.1. get and getline Member Functions

The get member function with no arguments inputs one character from the designated stream (including white-space characters and other nongraphic characters, such as the key sequence that represents end-of-file) and returns it as the value of the function call. This version of get returns EOF when end-of-file is encountered on the stream.

Using Member Functions eof, get and put

Figure 13.4 demonstrates the use of member functions eof and get on input stream cin and member function put on output stream cout. Recall from Chapter 5 that EOF is represented as an int. This program reads characters into the int variable character, so that we can test each character entered to see if it’s EOF. The program first prints the value of cin.eof()—i.e., false (0 on the output)—to show that end-of-file has not occurred on cin. The user enters a line of text and presses Enter followed by end-of-file (<Ctrl> z on Microsoft Windows systems, <Ctrl> d on Linux and Mac systems). Line 15 reads each character, which line 16 outputs to cout using member function put. When end-of-file is encountered, the while statement ends, and line 20 displays the value of cin.eof(), which is now true (1 on the output), to show that end-of-file has been set on cin. This program uses the version of istream member function get that takes no arguments and returns the character being input (line 15). Function eof returns true only after the program attempts to read past the last character in the stream.


 1   // Fig. 13.4: fig13_04.cpp
 2   // get, put and eof member functions.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      int character; // use int, because char cannot represent EOF
 9
10      // prompt user to enter line of text
11      cout << "Before input, cin.eof() is " << cin.eof() << endl
12         << "Enter a sentence followed by end-of-file:" << endl;
13
14      // use get to read each character; use put to display it
15      while ( ( character = cin.get() ) != EOF )
16         cout.put( character );
17
18      // display end-of-file character
19      cout << " EOF in this system is: " << character << endl;
20      cout << "After input of EOF, cin.eof() is " << cin.eof() << endl;
21   } // end main


Before input, cin.eof() is 0
Enter a sentence followed by end-of-file:
Testing the get and put member functions
Testing the get and put member functions
^Z

EOF in this system is: -1
After input of EOF, cin.eof() is 1


Fig. 13.4. get, put and eof member functions.

The get member function with a character-reference argument inputs the next character from the input stream (even if this is a white-space character) and stores it in the character argument. This version of get returns a reference to the istream object for which the get member function is being invoked.

A third version of get takes three arguments—a built-in array of chars, a size limit and a delimiter (with default value ' '). This version reads characters from the input stream. It either reads one fewer than the specified maximum number of characters and terminates or terminates as soon as the delimiter is read. A null character is inserted to terminate the input string in the character array used as a buffer by the program. The delimiter is not placed in the character array but does remain in the input stream (the delimiter will be the next character read). Thus, the result of a second consecutive get is an empty line, unless the delimiter character is removed from the input stream (possibly with cin.ignore()).

Comparing cin and cin.get

Figure 13.5 compares input using stream extraction with cin (which reads characters until a white-space character is encountered) and input using cin.get. The call to cin.get (line 22) does not specify a delimiter, so the default ' ' character is used.


 1   // Fig. 13.5: fig13_05.cpp
 2   // Contrasting input of a string via cin and cin.get.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      // create two char arrays, each with 80 elements
 9      const int SIZE = 80;
10      char buffer1[ SIZE ];
11      char buffer2[ SIZE ];
12
13      // use cin to input characters into buffer1
14      cout << "Enter a sentence:" << endl;
15      cin >> buffer1;
16
17      // display buffer1 contents
18      cout << " The string read with cin was:" << endl
19         << buffer1 << endl << endl;
20
21      // use cin.get to input characters into buffer2
22      cin.get( buffer2, SIZE );                      
23
24      // display buffer2 contents
25      cout << "The string read with cin.get was:" << endl
26         << buffer2 << endl;
27   } // end main


Enter a sentence:
Contrasting string input with cin and cin.get

The string read with cin was:
Contrasting

The string read with cin.get was:
 string input with cin and cin.get


Fig. 13.5. Contrasting input of a string via cin and cin.get.

Using Member Function getline

Member function getline operates similarly to the third version of the get member function and inserts a null character after the line in the built-in array of chars. The getline function removes the delimiter from the stream (i.e., reads the character and discards it), but does not store it in the character array. The program of Fig. 13.6 demonstrates the use of the getline member function to input a line of text (line 13).


 1   // Fig. 13.6: fig13_06.cpp
 2   // Inputting characters using cin member function getline.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      const int SIZE = 80;
 9      char buffer[ SIZE ]; // create array of 80 characters
10
11      // input characters in buffer via cin function getline
12      cout << "Enter a sentence:" << endl;
13      cin.getline( buffer, SIZE );
14
15      // display buffer contents
16      cout << " The sentence entered is:" << endl << buffer << endl;
17   } // end main


Enter a sentence:
Using the getline member function

The sentence entered is:
Using the getline member function


Fig. 13.6. Inputting characters using cin member function getline.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.242.24