#include <iostream>
#include <string>
int main() {
std::string str;
// Prompt the user to enter a string
std::cout << "Enter a string: ";
// Read a line of text from the user and store it in 'str'
std::getline(std::cin, str);
// Display the entered string
std::cout << "You entered: " << str << std::endl;
return 0;
}
#include <iostream>
: This line includes the input/output stream library, allowing input and output operations.#include <string>
: This line includes the string library, which provides functionality for handling strings.int main() { ... }
: The main function where program execution begins.std::string str;
: Declares a variable named 'str' of type 'std::string', which will hold the input string.std::cout << "Enter a string: ";
: Outputs the prompt asking the user to enter a string.std::getline(std::cin, str);
: Reads a line of text entered by the user using std::getline
, storing it in the variable 'str'.std::cout << "You entered: " << str << std::endl;
: Outputs the entered string using std::cout
.return 0;
: Indicates successful completion of the main()
function.