#include<iostream> using namespace std; int main(){ char c; while((c=cin.peek()) != '!') cin.get(c); cout << "You entered '!'. The next character is "; cin.get(c); cout << c << endl; return 0; }The program terminates when you enter ! somewhere and hit enter key. If the program terminates, it always outputs:
You entered ‘!’. The next character is !because cin.peek() puts ! back to input stream for cin.get() to get it.
#include<iostream> using namespace std; int main(){ char c; cin.get(c); cin.putback('c'); cin.get(c); cout<<'\n'<<c<<" is the next input character.\n"; return 0; }Enter any character you want. The program, before terminating, outputs:
c is the next input characterIf you take out the first cin.get(c), then the program will not work.