468,511 Members | 1,691 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

problem storing user input into string

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

Jul 23 '05 #1
1 1432
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;

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bartosz Wegrzyn | last post: by
20 posts views Thread by titi | last post: by
11 posts views Thread by aljaber | last post: by
1 post views Thread by saytri | last post: by
6 posts views Thread by Volker Neurath | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.