Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I use cin with std::string??

Status
Not open for further replies.

isthisreallyit

Programmer
Jan 2, 2003
6
GB
Hi,


I want to do something like :

std::string ss;
cin >> ss;

but compiler complains. no operator found which takes a right-hand operand of type std::string .

please give the correct code, thanks.
 
Um, I take it that you are trying to use the String class from the standard library?

if so then just use

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



string ss;
cin >> ss;
 
Lemme take a wild guess: you're using <iostream.h>. Don't do that. Use <iostream>, no dot and no h.

<iostream.h> is old and not officially part of C++ anymore. It was part of pre-standard C++; coincidentally, std::string was not part of pre-standard C++ (nor was std, for that matter), and that's why <iostream.h> doesn't support an operator for std::string.

<iostream.h> is just provided for backward compatibility. Never use it in new programs. Always use <iostream>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top