0

After using cin.getline(), is it possible to compare the output to a string, and if so, how?

The sololearn cpp tutorial just teaches the basics and not stuff like taking userinput that includes spaces and periods. I looked up how to take that input, but when I compared that to a string, it returned false.

21st May 2020, 6:41 AM
Bentlysk
Bentlysk - avatar
2 odpowiedzi
+ 2
In c++ there are 2 different versions of getline() available. There is cin.getline() (or std::istream::getline()) and std::getline(). cin.getline() is a C function and will result in a C string where getline() is for C++ string types. http://www.cplusplus.com/reference/istream/istream/getline/ http://www.cplusplus.com/reference/string/string/getline/ With a C string you need to use the strcmp() function to compare C strings. http://www.cplusplus.com/reference/cstring/strcmp/ With a C++ string you can use the strings compare method in addition to using relational operators. http://www.cplusplus.com/reference/string/string/compare/
21st May 2020, 10:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thank you!
21st May 2020, 2:56 PM
Bentlysk
Bentlysk - avatar