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++.
#include <iostream>
#include <cctype>
int main()
{
std::cout << "Enter a sentence: ";
//Peek at the next character in the input stream
int Ch = std::cin.peek();
// Printing the next character
if (Ch != EOF)
{
std::cout << "Next character in input: ";
if (std::isprint(Ch))
{
std::cout << static_cast<char>(Ch) << std::endl;
}
else
{
std::cout << "ASCII value for a non-printable character: " << Ch << ")" << std::endl;
}
}
else
{
std::cout << "End of input reached." << std::endl;
}
return 0;
}
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++.
#include <iostream>
int main()
{
std::cout << "Enter a number: ";
//Peek is the next character to enter the stream.
int Ch = std::cin.peek();
// Check if the next character is a digit
if (std::isdigit(Ch))
{
int n;
std::cin >> n;
std::cout << "You entered a number is: " << n << std::endl;
}
else
{
std::cout << "Invalid input. Expected a number." << std::endl;
}
return 0;
}
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++.
#include <bits/stdc++.h>
using namespace std;
int main()
{
istringstream demo("Engineering");
char l1 = demo.peek();
char l2 = demo.get();
char l3 = demo.get();
char l4 = demo.get();
cout << "The first character in the word is: "
<< l1 << endl
<< "and the next character in the word is: "
<< l3 << endl
<< "after that next character in the word is: "
<< l4 << endl;
}
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.