scan line in c++
Sure! Here is a response:
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
cout << "Enter a line of text: ";
getline(cin, line);
cout << "You entered: " << line << endl;
return 0;
}
Explanation:
- We start by including the necessary header files for input/output and string manipulation.
- Inside the main function, we declare a variable line
of type string to store the input.
- Then, we prompt the user to enter a line of text using the cout
statement.
- We use the getline
function to read the entire line of text entered by the user, and store it in the variable line
.
- Finally, we output the entered line of text using the cout
statement and the variable line
.
- The program then returns 0 to indicate successful execution.