GETLINE()

The getline() function is used to read an entire line of text, including spaces, from an input stream (such as std::cin or a file) into a std::string variable. Unlike the >> extraction operator, which stops reading at whitespace, getline() continues until it encounters a newline character (\n). This makes it useful for handling input that may include spaces, such as full names or sentences.


getline(std::istream& is, std::string& str, char delim);

 * istream is often cin
 * string is where the line is stored
 * delim is an optional custom delimiter (defaults to newline \n), allowing control 
   over how input is parsed
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string name;
  cout << "Enter your full name: ";
  getline(cin, name);
  cout << "Hello, " << name << "!" << endl;
  return 0;
}

Last updated