get line C++

The getline() function in C++ is used to read a line of text from an input stream. It is commonly used to get user input from the console.

Here is the syntax for using getline() function in C++:

getline(input_stream, variable_to_store_input);

The input_stream parameter is the stream from which the input is read, such as cin for standard input. The variable_to_store_input parameter is the variable in which the input will be stored.

When getline() is called, it reads characters from the input stream until it encounters a newline character ('\n') or reaches the end of the stream. The newline character is not stored in the variable_to_store_input.

Here is an example of how getline() can be used:

include

include

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

return 0; }

In this example, the getline() function is used to read a line of text from the standard input stream (cin) and store it in the variable "name". The input is then printed out with a greeting message.

Note that the getline() function can also be used with other input streams, such as file streams (ifstream) or string streams (stringstream), depending on the specific use case.