+ 4
Do you know what is the difference between 'gets' and 'cin' in C++?
3 Answers
+ 13
cin is a static object of the istream class, defined in the iostream header file. It is used for taking user input, like so:
int a;
cin >> a;
You can use it for taking input for any datatype by overloading the right-shift operator (>>) accordingly for that datatype. You donât need to do this for primitive types like int, float, char etc.
gets is a function, defined in the stdio.h header file. gets is used for taking string input, like so:
char t[100];
gets(t);
Although you can use cin for taking string input too, the input string will be truncated at the first white-space character.
Also, gets is now deprecated, meaning that future versions of C/C++ may not support it. This is because it does not check for buffer overflow. You may want to have a look at the fgetsfunction.
+ 2
Cin>> is the best đđ