+ 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.
55 Antworten
+ 4
use this.
string name;
getline (cin, name);
/* any whitespaces with normal cin causes problems. */
edit
also, you will have problems with << operator.
+ 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;
+ 7
cout is of type ostream
cin is of type istream
+ 7
@anubhav
Well to be clear, cin and cout cant have a return type. They are not functions.
+ 7
The class I/O libraries have good notes.
I will go over them, and see what I can use.
+ 7
@kinshuk. you are correct. you knew what i meant though right
+ 7
@anunhav. cool. you can also use this method to check for invalid input by preceeding cin with ! in the if statement
+ 7
😉
+ 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
+ 6
@anubhav: sorry for the confusion. we are discussing what data type cin is. not its return type or lack thereof.
+ 6
@anubhav pandey
Though cin cannot return anything, the operator >> does.
Its prototype:
friend istream& operator>>(istream& is, /*any thing to write*/);
+ 6
@kinshuk: Well that answers that question then!
+ 5
huh... it has a type??? google to the rescue. char
http://www.cplusplus.com/reference/iostream/cin/
i give this question 30 cookies
+ 5
I was of the understanding that cout and cin are global variables contained in iostream. but I have been wrong before
+ 5
@Manual: at the cin entry itself: http://www.cplusplus.com/reference/iostream/cin/
Then at istream class:
http://www.cplusplus.com/reference/istream/istream/
+ 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
+ 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);
+ 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...
+ 4
@Manual
cin and cout are global objects of the type i/o stream that have overloaded the >> and << operators with a function.
+ 4
Oh okay.