Jump to content
  • Advertisement
Sign in to follow this  
TheOne1

string class and cin

This topic is 5048 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
Advertisement
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

#include <iostream>
#include <string>
using namespace std;

int main()
{
string YourString = "";

getline(cin, YourString);
cout<<"Your String = "<<YourString.c_str()<<endl;
return 0;
}




That works

Edit:
NewbJ beat me by 11secs :)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!