cin >> “no operator matches these operands”

I’ve been working on a C++ project in visual studio 2012 console mode and I keep getting this strange persistent error with the cin function. Under the >> I get a red line and the program tells me no operator matches these operands. I have initialized all the array elements in a separate method. Here’s a snippet example (The actual … Read more

Why would we call cin.clear() and cin.ignore() after reading input?

The cin.clear() clears the error flag on cin (so that future I/O operations will work correctly), and then cin.ignore(10000, ‘\n’) skips to the next newline (to ignore anything else on the same line as the non-number so that it does not cause another parse failure). It will only skip up to 10000 characters, so the code is assuming the user will … Read more

When and why do I need to use cin.ignore() in C++?

Ignore is exactly what the name implies. It doesn’t “throw away” something you don’t need instead, it ignores the amount of characters you specify when you call it, up to the char you specify as a breakpoint. It works with both input and output buffers. Essentially, for std::cin statements you use ignore before you do a getline call, because … Read more

What are the rules of the std::cin object in C++?

What is happening here is that std::cin >> firstName; only reads up to but not including the first whitespace character, which includes the newline (or ‘\n’) when you press enter, so when it gets to getline(std::cin, articleTitle);, ‘\n’ is still the next character in std::cin, and getline() returns immediately. Adding ‘std::cin >> std::ws‘ (ws meaning … Read more