C++ User Input

Include the following code to read user input in C++:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string userInput;
    cout << "Enter your input: ";
    getline(cin, userInput);
    cout << "You entered: " << userInput << endl;
    return 0;
}
  1. The #include <iostream> line allows you to perform input and output operations.
  2. #include <string> provides functionality for string handling.
  3. using namespace std; allows you to use items in the standard C++ library.
  4. In the main() function, string userInput; declares a variable to store the user input.
  5. cout << "Enter your input: "; displays a message asking for input.
  6. getline(cin, userInput); reads a line of text from the user and assigns it to the userInput variable.
  7. cout << "You entered: " << userInput << endl; displays the entered input.
  8. return 0; ends the program.