getline() does not work if used after some inputs

haracters are extracted until either (n – 1) characters have been extracted or the delimiting character is found (which is delimiter if this parameter is specified, or ‘\n’ otherwise). The extraction also stops if the end of the file is reached in the input sequence or if an error occurs during the input operation. When cin.getline() reads … 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