#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::string name;
std::cin >> name ; // formatted input: this leaves trailing white space in the input buffer
std::cout << "name1: " << name << '\n' ;
// std::cin >> std::ws: read the next line (unformatted input with std::getline) after
// extracting and discarding the trailing white space characters (including the new line)
// remaining in the input buffer (the tail of the previous line) after the last formatted input
std::getline( std::cin >> std::ws, name ) ;
std::cout << "name2: " << std::quoted(name) << '\n' ;
}
Line 6 is wrong. It should just be "cin.ignore();". The ignore works on "cin" not name. A better and more often used way of writing line 6 would be: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
Using line 6 in this code is not needed with just a getline. The only reason for using ".ignore" is when "getline" follows "cin >> aVariable;" because this input will leave the newline in the input buffer. That is when the ".ignore" is used to clear the input buffer before a "getline". On the other hand "getline" will extract the newline from the input buffer.