problem storing user input into string

K

Kelly Mandrake

I've learned that some compilers include a string class, and I have
been reading about it. I can use it in the normal way but I have a
question. If I want to have user input stored into my string. I tryed
to use cin.get But for some reason I cannot get the user input into my
string. I first tryed cin.get(name); but I noticed soon that get
method looks for a char* and not a std::string. So I learned that
string has a method that returns a pointer to a const char. How could
I make this work?

string name;
cout << "Name the cat: ";
cin.get((char*)name.c_str(), 10);
cout << "Name is " << name << endl;

output: Name is
 
K

Kelly Mandrake

I solved my problem this time. I found yet another refernce to the
sting class and learned of a function getline that solved my problems

string name;
cout << "Name the cat: ";
getline(cin, name);
cout << "Name is " << name << endl;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top