Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am a newbie in C++ programming and I'm trying to use std:: because someone told me that is a good habit rather than putting in using namespace std; because it pollutes the global namespace. I'm not sure why std::cin >> name; from my code below produce an error no operator '>>' matches these operands below is the full source code.

#include "stdafx.h"
#include <ios>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    int x, y;
    std::string name;

    std::cin >> name;
    std::cin >> x;

    return 0;
}
share|improve this question
This belongs in StackOverflow.. you are missing the include for strings – K-ballo Jan 6 at 18:02
@K-ballo thanks how do I close this question? – Rojee Jan 6 at 18:04

closed as off topic by Jeff Vanzella, Corbin, Jerry Coffin, Glenn Rogers, William Morris Jan 10 at 20:42

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

You are just missing the definition of std::string.

Add

#include <string>

Note:

std::cin >> name; // Will read a space separated word.

This is fine for inputs of known format.
But when you are doing interactive programming with the user I personally find it better to get a line at a time:

std::getline(std::cin, name);

Then you can error check to see if he entered two words or one etc.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.