What is difference between cin, cin.get(address,size) and cin.getline(address,size)

According to

  1. https://en.cppreference.com/w/cpp/io/basic_istream/get
  2. https://en.cppreference.com/w/cpp/io/basic_istream/getline

We can find the statement "If no characters were extracted, calls setstate(failbit)."

I can understand it with the code below

#include <iostream>

using namespace std;
const int ArrSize = 400;

int main()
{
   char arr1[ArrSize];
   char arr2[ArrSize];
   char arr3[ArrSize];

   cout << "enter the first string ";
   cin >> arr1;
   cout << "enter the second string ";
   cin.get(arr2, ArrSize);
   cout << "enter the third string ";
   cin>>arr3;


   cout << endl << endl;

   cout << "first string is: " << arr1 << "\n";
   cout << "second string is:  " << arr2 << "\n";
   cout << "third string is:  " << arr3 << "\n";

   return 0;
}

Now, what I'm curious is how about cin>>? Does cin>> also have that trait or not?



Read more here: https://stackoverflow.com/questions/68456669/what-is-difference-between-cin-cin-getaddress-size-and-cin-getlineaddress-si

Content Attribution

This content was originally published by CD S at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: