+ 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 šš