cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : istream : putback
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
istream
· istream::istream
· istream::~istream
member classes:
· istream::sentry
member functions:
· istream::gcount
· istream::get
· istream::getline
· istream::ignore
· istream::operator>>
· istream::peek
· istream::putback
· istream::read
· istream::readsome
· istream::seekg
· istream::sync
· istream::tellg
· istream::unget

-

istream::putback public member function
istream& putback ( char c );

Put character back

Decrements the internal get pointer by one, and c becomes the character to be read at that position by the next input operation.

The function effectively calls the sputbackc member function of the streambuf object associated to the stream.

A subsequent call to member gcount will return zero.

Parameters

c
The character to be put back.

Return Value

The function returns *this

Errors are signaled by modifying the internal state flags:

flagerror
eofbit-
failbitThe stream was at the end of the source of characters before the function was called.
badbitAn error other than the above happened.

Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.

Example

// istream putback
#include <iostream>
using namespace std;

int main () {
  char c;
  int n;
  char str[256];

  cout << "Enter a number or a word: ";
  c = cin.get();

  if ( (c >= '0') && (c <= '9') )
  {
    cin.putback (c);
    cin >> n;
    cout << "You have entered number " << n << endl;
  }
  else
  {
    cin.putback (c);
    cin >> str;
    cout << " You have entered word " << str << endl;
  }

  return 0;
}

Basic template member declaration

( basic_istream<charT,traits> )
typedef charT char_type;
basic_istream& putback ( char_type c );

See also

istream::get Get unformatted data from stream (public member function)
istream::unget Decrement get pointer (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us