Loading....
Coupon Accepted Successfully!

 

Member Functions of the istream Class

The istream contains the following functions that can be called using cin object.

peek(): It returns the succeeding character without extraction. For example,

 

cin.peek()==‘#’;

 

where cin is an object and ‘#’ is the symbol that is to be caught in the stream.

ignore(): The member function ignore() has two arguments, maximum number of characters to avoid and the termination character. For example,

 

cin.ignore (1,‘#’);


The statement ignores character 1 till character ‘#’is found.

 
3.26 Write a program to demonstrate the use of peek() and ignore() functions.

Explanation: In the above program, the cin.get() function continuously reads characters through the keyboard till the user presses F6. The cout statement inside the loop displays the contents of variable c on the console. The cin.peek() statement checks the variable c. If the variable c contains ‘#’, it is ignored from the stream and not displayed on the screen.

 

putback(): The putback() replaces the given character into the input stream. For example,

 

cin.putback (‘*’);

 

where cin is an object and ‘*’ is the symbol which is replaced in the stream.


3.27 Write a program to demonstrate the use of putback() function.

Explanation: In the above program, the cin.get() function continuously reads characters through the keyboard till the user presses F6. The cout statement inside the loop displays the contents of the variable c on the console. The if statement checks the contents of the variable c. If variable c contains a small letter ‘s’, the putback() statement sends capital ‘S’ in the stream. The small ‘s’ is replaced with capital ‘S’. The contents displayed will be with capital ‘S’.

 

Gcount(): This function returns the number of unformatted characters extracted from the input stream. The last statement should be get(), getline(), or read().

3.28 Write a program to demonstrate the use of gcount() function.

Explanation: In the above program, the getline() function reads text through the keyboard. The gcount() function returns the number of characters extracted from stream to variable len. It also counts the null character. The cout statement displays the value of variable len on the screen.

3.29 Write a program to perform the operation with peek() and putback().

Explanation: In the above program, the cin.get() function reads data through the keyboard using first while loop. The if condition checks for blank space in the entered text. If space is found, the putback() statement replaces space dot (.). The putback() sends the given character in the input stream. The peek() also checks the ‘#’ symbol in the entered text, if it is found, the ignore() statement ignores the character and the character will not be displayed on the screen. The program is terminated when the key F6 or ctrl+z is pressed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name