471,047 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,047 software developers and data experts.

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 1497
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.