In this guide, you will explore the fundamental_istream::peek function in C++ along with its syntax, purpose, and illustrations.
What is the basic_istream::peek method?
In C++, you can inspect the upcoming character in the input stream without extracting it by utilizing the peek function. This function is a part of the input stream classes, such as std::basic_istream. The peek method is commonly employed to check the next character in the input stream and make decisions based on its value before extraction.
It is frequently employed in parsing algorithms, input validation, and managing unique scenarios during 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 subsequent character in the input stream can be observed without extraction by utilizing the peek function.
It provides an integer value representing the subsequent character in the input stream, or EOF if there are no more characters accessible.
Return Value:
The subsequent character in the stream is denoted by an integer within the peek result, or EOF if the stream's conclusion is encountered.
It does not eliminate the character from the stream; instead, it offers a preview of what is coming next.
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's consider a scenario to demonstrate the fundamental usage of the basic_istream::peek function 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:
When following this example, the upcoming character in the input stream will be previewed and displayed after a request to formulate a sentence. Displaying the character will happen only if it is a printable character. If the subsequent character (like a space) is unprintable, the message "This character is not printable" along with its corresponding ASCII value will be shown. Upon reaching the end of the input, the message "End of input reached" will be presented.
Example-2
Let's consider another instance to demonstrate the basic_istream::peek function 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 program written in C++ guides the user to input a numerical value. Subsequently, it inspects the following character in the input sequence without removing it by leveraging the std::cin peek method. The variable Ch holds the value retrieved by peek. It then employs the std::isdigit function to ascertain whether the character represented by Ch is indeed a digit. In the event that it is a digit, the program proceeds to utilize std::cin >> n for extracting the integer, indicating that the user has entered a numeric character. A confirmation message regarding the entered number is exhibited, displaying both the acknowledgment and the actual input stored in the variable n. Conversely, if the subsequent character is not a digit, the program issues an error message conveying that the input is invalid since a numerical input was anticipated. Eventually, the program concludes by returning 0, denoting successful execution.
Example-3
Let's consider another instance to demonstrate the fundamental usage of the basic_istream::peek function 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 instance, characters are fetched from the stream utilizing the get function, while the subsequent character in the input stream is inspected using the peek method without retrieval. This showcases how C++ programs can manipulate input streams through these techniques.