get() funct to get user string input | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

get() funct to get user string input

Why it display error when i run it? I used ‹string> header and cin.get() func... is this wrong?

28th Feb 2019, 4:31 AM
Xtron135
Xtron135 - avatar
4 Réponses
28th Feb 2019, 5:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
The get method is used to read single characters or a c-string. It does not work for strings. To read a string, use getline with the following syntax: getline(<input_stream>, <reference_to_string>, [delimiter]); Eg: string s; getline(std::cin,s); For more details: http://www.cplusplus.com/reference/istream/istream/get/ http://www.cplusplus.com/reference/string/string/getline/
28th Feb 2019, 4:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Thanks Kinshuk.. just wanna ask, is this only for sololearn compiler or valid for all compiler? I think i can use the get() func in a normal destop c++ compiler..
28th Feb 2019, 4:49 AM
Xtron135
Xtron135 - avatar
+ 2
This should have the same behaviour on all platforms, as this is how the get method is defined for the istream class under the header istream. There is no overload present for C++ strings, as those came later. If possible, can you share the code that used get for strings and compiled on your PC?
28th Feb 2019, 4:56 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar