Basic_istream::peek() method in C++In this article, you will learn about the basic_istream::peek() method in C++ with its syntax, functionality, and examples. What is the basic_istream::peek() method?In C++, the next character in the input stream can be examined without being extracted using the peek() method. It is a member function of the input stream classes (std::basic_istream). You frequently use peek when examining the next character in the input stream to determine what to do depending on its peek(). It's commonly used in parsing algorithms, input validation, and handling of special cases in input processing. Syntax:It has the following syntax:
Functionality:The next character in the input stream can be seen without being removed from the stream using the peek() method. It returns an integer value for the next character in the input stream, or EOF, if no more characters are available. Return Value:The next character in the stream is represented by an integer in the peek() return value, or EOF if the stream's end is reached. It does not remove the character from the stream; it merely provides a look-ahead. Characteristics:
Example-1Let us take an example to illustrate the basic_istream::peek() method in C++. Output: Enter a sentence: God is Great Next character in input: G Explanation: In this example, the next character in the input stream will be peeked at and printed after it prompts you to create a sentence. Printing of the character itself will occur if the next character is printable. The message "This character is not printable", and its ASCII value will be printed if the next character (such as whitespace) cannot be printed. The message "End of input reached" will display when the input ends. Example-2Let us take another example to illustrate the basic_istream::peek() method in C++. Output: Enter a number: 8 You entered a number is: 8 Explanation: The user is prompted to input a number using this C++ program. After that, the next character in the input stream is examined without being extracted using the std::cin peek() method. The variable Ch stores the value that peek() returned. After that, it uses the std::isdigit() function to determine whether the character that Ch represents is a digit. If so, the program uses std::cin >> n to extract the integer, displaying that the user has typed a numeric character. The program displays a message verifying the number entered along with the entered number and stores the entered number in variable n. However, if the next character is not a digit, the program outputs a message stating that the input is invalid, as it expected a number. Finally, the program returns 0 to indicate successful execution. Example-3Let us take another example to illustrate the basic_istream::peek() method in C++. Output: The first character in the word is: E and the next character in the word is: n after that next character in the word is: g Explanation: In this example, characters are extracted from the stream using the get() function, and the next character in the input stream is examined using the peek() method without extracting it. This demonstrates how C++ programmes may change input streams using these methods. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India