Comparing cin and cin.get to read string - C++ File Stream

C++ examples for File Stream:cin

Description

Comparing cin and cin.get to read string

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    // create two char arrays, each with 80 elements
    const int SIZE = 80;
    char buffer1[SIZE];
    char buffer2[SIZE];

    // use cin to input characters into buffer1
    std::cout << "Enter a sentence:" << std::endl;
    std::cin >> buffer1;//ww  w  .j  a va  2s.com

    // display buffer1 contents
    std::cout << "\nThe string read with cin was:" << std::endl
              << buffer1 << std::endl
              << std::endl;

    // use cin.get to input character into buffer2
    std::cin.get(buffer2, SIZE);

    // display buffer2 contents
    std::cout << "The string read with cin.get was:" << std::endl
              << buffer2 << std::endl;

    return 0;
}

Result


Related Tutorials