Sign in to follow this  

string class and cin

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do you read characters/strings into a string object? For some reason, cin.getline() will not work with string class objects. I get this error: :\Program Files\Microsoft Visual Studio\MyProjects\tester\mainTest.cpp(17) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : can not convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Share this post


Link to post
Share on other sites
this should work:

getline(cin, myString);

or

std::getline(std::cin, myString);

someone else could explain exactly how that works...I know it has to do with the fact that the normal getline only accepts c-style strings, but I'm not sure why this form does

Share this post


Link to post
Share on other sites
Quote:
Original post by NewbJ
this should work:

getline(cin, myString);

or

std::getline(std::cin, myString);

someone else could explain exactly how that works...I know it has to do with the fact that the normal getline only accepts c-style strings, but I'm not sure why this form does


That works but....I have to press enter twice. Why?

Share this post


Link to post
Share on other sites

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this