getline trong c++
getline
is a function in C++ that is used to read a line of text from an input stream. It is commonly used to read user input from the standard input stream (cin
), but it can also be used to read from other input streams like files.
Here is an explanation of each step involved in using getline
in C++:
Include the necessary header file:
cpp #include <iostream>
This line includes the necessary header file<iostream>
which contains the definition ofgetline
function.Declare a string variable to store the input:
cpp std::string input;
This line declares a variableinput
of typestd::string
to store the line of text read from the input stream.Use
getline
to read the input:cpp std::getline(std::cin, input);
This line uses thegetline
function to read a line of text from the standard input stream (std::cin
) and stores it in theinput
variable.Process the input as needed:
cpp // Example: Print the input std::cout << "You entered: " << input << std::endl;
This line demonstrates an example of processing the input. In this case, it simply prints the input back to the standard output stream (std::cout
).
That's it! The above steps cover the basic usage of getline
in C++. You can modify and extend this example to suit your specific needs.