Read a line of string with getline(), a character with get(), output with put() via cin - C++ File Stream

C++ examples for File Stream:cin

Description

Read a line of string with getline(), a character with get(), output with put() via cin

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    int character;  // user int, because char cannot represent EOF

    // prompt user to enter line of text
    std::cout << "Before input, cin.eof() is " << std::cin.eof() << std::endl
              << "Enter a sentence followed by end-of-file:" << std::endl;

    // use get to read each character; use put to display it
    while ((character = std::cin.get()) != EOF) 
       std::cout.put(character);//from   ww  w .j av a  2 s  . c om

    // display end-of-file character
    std::cout << "\nEOF in this system is: " << character << std::endl;
    std::cout << "After input of EOF, cin.eof() is " << std::cin.eof()
              << std::endl;
    return 0;
}

Result


Related Tutorials