A little he*p with string.h

string str;
getline(cin,str);


So I saw this piece of code and I had the following queries ...

1] What does the string keyword do ? Is it like another data type to store strings ?

2] When is using the getline() more advantageous and when is using cin and storing the string in an array advantageous ? Even if you merely mention examples it'd help a lot

And for question 1, I did check information in the reference section , but I could't understand....

Thanks in advance....
string is not a keyword, it's a class defined in the standard library. A managed dynamic string. The great thing about C++'s managed strings is that they can use characters of any size (8 bits, 16 bits, 32 bits, etc.). Not all languages have this feature.
Using getline() with a string lets you input lines of any length, so you don't have to worry about buffer overflows. getline() also defeats the space problem with std::cin (e.g. if you do std::cin >>Cstring; and input "Hello, World!", Cstring will only contain "Hello,").
Last edited on
strings are allways better that character array as they don't have a fixed length and they can have '\0' characters in them.
so what would be the pros and cons of using gets() with respect to getline() ?

Reason for edit : forgot the '?'
Last edited on
I think gets() is C and it doesn't allow to choose the ending character, getline() is used for C++ strings and it is more flexible

For more documentation, read:
http://www.cplusplus.com/reference/clibrary/cstdio/gets.html
http://www.cplusplus.com/reference/string/getline.html
thank you for posting every one !
Topic archived. No new replies allowed.