4

I'm learning C++ by following examples in a book and after typing this out and double checking I keep getting error messages. I cant figure out what is wrong. I'm using Visual C++ 2010 if it matters.

#include <iostream>
using namespace std;

int main()
{
// Prompt the user for data 
cout << "Please enter two words:" << endl;

//Read in the values
string b, c;
cin >> b >> c;

// Give feedback
cout << "I understood: "
     << b << ", and "
     << c << endl;

// NOw, lets's read a whole line of text as a single entity
cout << "Now, type in a whole line of text, "
     << "with as many blanks as you want:"
     << endl;

//getline() is a function; we'll talk more about them in Part3 
string wholeLine;
getline( cin, wholeLine );

//In the cout statement below, remember that \"
// is an escape sequence!
cout << "I understood: \"" << wholeLine << "\"" << endl;

// And we're done! 
return 0;
}

There are four errors. The error codes are:

Error 1 error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) i:\helloworld.cpp 11

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) i:\helloworld.cpp 15

Error 3 error C3861: 'getline': identifier not found i:\helloworld.cpp 25

Error 4 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) i:\helloworld.cpp 29

2 Answers2

7

Missing #include <string> for string.

wesley.mesquita
  • 795
  • 5
  • 12
  • This is it. `` defines its operators `<<` and `>>`. – wjmolina Feb 06 '14 at 18:18
  • 2
    Thanks, that was the issue. I followed the book word for word and it doesn't show this. I think I better find a new book to use. – user3280763 Feb 06 '14 at 18:21
  • 2
    @user3280763 what is the book? – wesley.mesquita Feb 06 '14 at 18:21
  • Learn C++ By Making Games by Yuzmwa/Laramee (ISBN:1-58450-455-2) The sad part is this is only chapter 3. – user3280763 Feb 06 '14 at 18:25
  • 2
    @user3280763: Have a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Fred Larson Feb 06 '14 at 18:26
  • I'll bet it still doesn't work (or there's some additional code you haven't shown). – James Kanze Feb 06 '14 at 18:29
  • @user3280763 That book list is great but can be a bit overwhelming! I, personally, suggest "C++ primer plus(6th ed) by Stephen Prata". Have a look at it. – Jatin Feb 06 '14 at 18:30
  • @Jatin I would highly recommend Stroustrup's book _Programming: Principles and Practice using C++_. – James Kanze Feb 06 '14 at 18:34
  • thanks for all the book suggestions, I'll definitely look into them. @ James Kanze: that's all the code and it works without error but it doesn't allow me to type in the string. I'll mess with it some more before calling it quits and getting a new book. – user3280763 Feb 06 '14 at 18:38
  • @JamesKanze I haven't read it! Although, my experience with books by authors of languages hasn't been the best! I mean, K&R comes highly recommended for C, but I didn't find it suitable for very beginners! – Jatin Feb 06 '14 at 18:38
  • 1
    @Jatin K&R (and the earlier Stroustrup) are excellent books, but they don't target beginners. _Programming: Principles and Practice using C++_ does; it was designed for people who know nothing about programming, and how to write programs, and it addresses those issues more than just pure C++. – James Kanze Feb 07 '14 at 09:52
1

As Wesley already said, ensure you have the library included.

Also, be very careful when using getline() and cin in the same program. One can affect the other and you can get unexpected results. To be safe, I've found that it is useful to use cin.ignore() after using a cin and before using a getline. Cheers!

Llurendt
  • 23
  • 6