<<<Index>>>

Input/Output with String Stream



    #include <cassert>
    #include <iostream>
    #include <sstream>
    using namespace std;
    int main()
    {
        stringstream buffer;
        int onechar; // because char cannot represent EOF
        cout << "Enter some text: ";
        while ( ( onechar = cin.get() ) != EOF ) {
            if ( onechar == '\n' ) {
                break; // exit loop at the end of line
            } else if ( isalpha( onechar ) ) {
                buffer << "alpha: " << char( onechar ) << '\t' << onechar <<'\n';
            } else if ( isdigit( onechar ) ) {
                buffer << "digit: " << char( onechar ) << '\t' << onechar <<'\n';
            } else {
                buffer << "other: " << char( onechar ) << '\t' << onechar <<'\n';
            }
        }
        cout << buffer.str() << '\n'; // str() returns string
        return 0;
    }

<<<Index>>>