[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

    Search the Q&A Archives

My compiler(VC++ 6) never accepts cin >> str1 if str1...

<< Back to: C++ FAQ (part 1 of 10)

Question by meena
Submitted on 8/3/2003
Related FAQ: C++ FAQ (part 1 of 10)
Rating: Rate this question: Vote
My compiler(VC++ 6) never accepts cin >> str1 if str1 is declared as a string .
the following program gives error.
#include <iostream.h>
#include <string>

using namespace std;

int main() {
string testStr;
cout << "enter string";
cin >> testStr;

return 0;
C:\Program Files\Microsoft Visual Studio\MyProjects\Learning\test2\test_loop.cpp(9) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allo
cator<char> >' (or there is no acceptable conversion)

i dont have my online help installed properly. so i am unable to use string type. can you comment?

Answer by Mason Deaver
Submitted on 8/8/2003
Rating:  Rate this answer: Vote
Change "#include <iostream.h>" to "#include <iostream>".  You should use the newer standard headers such as iostream, string and fstream instead of iostream.h, string.h and fstream.h.  Mixing older and new style headers causes compilation problems such as you experienced.


Answer by DString
Submitted on 1/14/2006
Rating: Not yet rated Rate this answer: Vote
Depending on the compiler you are using you don't need the "using namespace std". Visual Studio has a problem with this depending upon how you use it.


Answer by Rob Fries
Submitted on 11/3/2006
Rating: Not yet rated Rate this answer: Vote
To further elaborate:  cin and cout are based on character arrays (char *) rather than true 'string' class objects.  cin is supplying a pointer to a null-terminated char array, but testStr (under the old iostream.H) won't accept that type.  Mason is correct; only use <iostream. from now on.


Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.

Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.


FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.


<< Back to: C++ FAQ (part 1 of 10)

[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.