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:

int peek();

  • The method's return type is int, and it uses an integer value to indicate the next character in the input stream.
  • The peek() is the name of the method.
  • It doesn't take any parameters.

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:

  • The character returned by the next read operation will be the same because peek() does not consume or remove characters from the input stream.
  • The non-destructive procedure means you can examine the input without advancing the stream position.
  • When a later read operation consumes the character that peek() returned, it remains in the input stream.

Example-1

Let 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-2

Let 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-3

Let 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.






Latest Courses