+ 7

What is the data type of cin?

For example string name; cin >> name; // Error // You will get this error outside SL code playground but when Int num; cin >> num; // ok I am using Visual C++ compiler. Thanks for helping me friends.

5th Jun 2017, 2:52 AM
Siddharth Saraf
44 Answers
+ 4
use this. string name; getline (cin, name); /* any whitespaces with normal cin causes problems. */ edit also, you will have problems with << operator.
5th Jun 2017, 3:12 AM
Manual
Manual - avatar
+ 11
Even the code in your question Int num; cin>>num; cout>>num; Will produce error. It should be int num; cin>>num; cout<<num;
5th Jun 2017, 5:36 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 7
cout is of type ostream cin is of type istream
5th Jun 2017, 2:54 AM
Rrestoring faith
Rrestoring faith - avatar
+ 7
@anubhav Well to be clear, cin and cout cant have a return type. They are not functions.
5th Jun 2017, 4:58 AM
Rrestoring faith
Rrestoring faith - avatar
+ 7
The class I/O libraries have good notes. I will go over them, and see what I can use.
5th Jun 2017, 4:59 AM
Manual
Manual - avatar
+ 7
@kinshuk. you are correct. you knew what i meant though right
5th Jun 2017, 5:09 AM
jay
jay - avatar
+ 7
@anunhav. cool. you can also use this method to check for invalid input by preceeding cin with ! in the if statement
5th Jun 2017, 5:24 AM
jay
jay - avatar
+ 7
😉
5th Jun 2017, 5:38 AM
jay
jay - avatar
+ 6
so if we had a char array of sufficient size could the extraction operator work on cin? edit: back to original question double edit: i doubt it. as the operator probably isnt overloaded for cin but thought i would ask
5th Jun 2017, 5:00 AM
jay
jay - avatar
+ 6
@anubhav: sorry for the confusion. we are discussing what data type cin is. not its return type or lack thereof.
5th Jun 2017, 5:08 AM
jay
jay - avatar
+ 6
@anubhav pandey Though cin cannot return anything, the operator >> does. Its prototype: friend istream& operator>>(istream& is, /*any thing to write*/);
5th Jun 2017, 5:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 6
@kinshuk: Well that answers that question then!
5th Jun 2017, 5:20 AM
jay
jay - avatar
+ 5
huh... it has a type??? google to the rescue. char http://www.cplusplus.com/reference/iostream/cin/ i give this question 30 cookies
5th Jun 2017, 2:55 AM
jay
jay - avatar
+ 5
I was of the understanding that cout and cin are global variables contained in iostream. but I have been wrong before
5th Jun 2017, 4:22 AM
jay
jay - avatar
5th Jun 2017, 4:44 AM
jay
jay - avatar
+ 5
nope! I am moving away from console atm. Havent used cout or cin for a bit. Using streams a bit but for working with files
5th Jun 2017, 4:53 AM
jay
jay - avatar
+ 5
They are objects. If not, why are we able to use: cin.ignore(); getline(cin, string); And getline has a prototype: istream& getline( istream& is, string& str, char delim);
5th Jun 2017, 4:54 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
@jay I think extraction operator is >> and not << acc to this: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx And >> is default for cin, isn't it? And I dont think cin<<var; is valid at all... I mean, can you write data to a file opened in read only mode? Eg - ifstream fin; fin.open("a.txt"); fin<<"Hello"; //I dont think its possible...
5th Jun 2017, 5:07 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
@Manual cin and cout are global objects of the type i/o stream that have overloaded the >> and << operators with a function.
5th Jun 2017, 3:15 AM
Rrestoring faith
Rrestoring faith - avatar
+ 4
Oh okay.
5th Jun 2017, 3:26 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar