basic_istream::putback() in C++
The basic_istream::putback() method in C++ is a component of the basic_istream template and a member of the input stream class hierarchy. This function is intended to alter input streams by inserting a new character into the stream. A character is eliminated from the stream when it is taken out using methods like get() or >>. But occasionally, it could be essential to put the character back into the stream so that it can be processed again. It is used where the use of putback() is necessary. A single character can be restored to the beginning of the input stream using the putback() function, freeing it up for the following input action. It can be especially helpful in situations where reevaluating a character is necessary for parsing or tokenizing data. Developers can increase the flexibility and precision of input stream manipulations in C++ programs by employing putback() to get more control over the input process.
Methods Used Are:
- basic_istream::putback(): The basic_istream template is a member function that is used to insert a character back into the input stream.
- get(): The introduction makes reference to scenarios in which characters are taken from the stream by use of functions such as get(), even if it does not state them specifically. Character extraction with such approaches necessitates the use of putback().
- Similar to that, the introduction suggests that the stream extraction operator (>>) be utilized to extract characters from the input stream. Use putback() to restore any characters that were extracted using this operator.
C++ Code:
Let's take an example to illustrate the use of the basic_istream::putback() function in C++.
#include <iostream>
int main() {
std::cout << "Enter a sentence: ";
// Create an input stream
std::istream& inputStream = std::cin;
// Read the first character using get()
char firstChar = inputStream.get();
// Output the first character
std::cout << "First character: " << firstChar << std::endl;
// Use putback() to put the character back into the stream
inputStream.putback(firstChar);
// Read the character again using get()
char reReadChar = inputStream.get();
// Output the re-read character
std::cout << "Character after putback(): " << reReadChar << std::endl;
return 0;
}
Output:
Enter a sentence: This is tutorial point and example
First character: T
Character after putback(): T
Example 2:
Let's take another example to illustrate the use of the basic_istream::putback() function in C++.
#include <iostream>
int main() {
std::cout << "Enter a sentence. Type 'x' to stop: ";
// Create an input stream
std::istream& inputStream = std::cin;
char currentChar;
// Read characters until 'x' is entered
while (true)
{
// Read a character using get()
currentChar = inputStream.get();
// Check if the user wants to stop (by entering 'x')
if (currentChar == 'x')
{
std::cout << "Stopping input." << std::endl;
break;
}
// Output the current character
std::cout << "Read: " << currentChar << std::endl;
// Put the character back into the stream
inputStream.putback(currentChar);
}
return 0;
}
Output:
Enter a sentence. Type 'x' to stop: THIS IS AN EXAMPLE PROGRAM WHICH IS GOING TO POST ON JAVATPOINT
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
Read: T
X
Example 3:
#include <iostream>
#include <limits> // For numeric_limits
int main()
{
std::cout << "Enter an integer: ";
// Create an input stream
std::istream& inputStream = std::cin;
int userInput;
// Read an integer using >>
while (!(inputStream >> userInput)) {
std::cout << "Invalid input. Please enter an integer: ";
// Clear the error flag
inputStream.clear();
// Ignore any remaining characters in the input buffer
inputStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Put back the first character to reprocess
inputStream.putback(' ');
}
// Output the user's input
std::cout << "You entered: " << userInput << std::endl;
return 0;
}
Output:
Enter an integer: 23
You entered: 23
34
dash: 2: 34: not found
Conclusion:
When a character is reinserted into an input stream, basic_istream::putback() in C++ offers a useful method for modifying streams of data. The ability to reprocess or rectify characters is made easier by this function, which also gives developers more freedom and control over input procedures. Error management and user interaction, such as enabling users to fix their input in the event of an error, can be combined effectively with the putback() function. C++ programs are more resilient with more dynamic and interactive input data handling made possible by this feature. Basic_istream::putback() is a versatile function that proves how versatile C++ is when it comes to controlling input operations, even though it may not be used often. It is particularly useful in circumstances that require precise control over the input stream.