public member function
<istream> <iostream>
std::basic_istream::ungetUnget character
Attempts to decrease the current location in the stream by one character, making the last character extracted from the stream once again available to be extracted by input operations.Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true
). Then (if good), it calls sungetc on its associated stream buffer object (if any). Finally, it destroys the sentry object before returning.
If the
eofbitflag is set before the call, the function fails (sets
failbitand returns).
The function clears the
eofbitflag, if set before the call.
Calling this function sets the value returned by gcount to zero.
*this
).
Errors are signaled by modifying the internal state flags:
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// istream::unget example
#include <iostream> // std::cin, std::cout
#include <string> // std::string
int main () {
std::cout << "Please, enter a number or a word: ";
char c = std::cin.get();
if ( (c >= '0') && (c <= '9') )
{
int n;
std::cin.unget();
std::cin >> n;
std::cout << "You entered a number: " << n << '\n';
}
else
{
std::string str;
std::cin.unget();
getline (std::cin,str);
std::cout << "You entered a word: " << str << '\n';
}
return 0;
}
Please, enter a number or a word: 7791 You entered a number: 7791
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4