read from standard input cpp

#include <iostream>
#include <string>

int main() {
    std::string name;
    int age;

    std::cout << "Enter your name: ";
    std::getline(std::cin, name);

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

    return 0;
}
  • #include <iostream>: Includes the input-output stream library, allowing input and output operations.
  • #include <string>: Includes the string library to handle string operations.

  • int main() { }: The main function where the program execution starts.

  • std::string name; and int age;: Declares two variables: name of type std::string to store the user's name, and age of type int to store the user's age.

  • std::cout << "Enter your name: ";: Displays the message "Enter your name: " on the console.

  • std::getline(std::cin, name);: Reads the entire line of text entered by the user after the prompt and stores it in the variable name.

  • std::cout << "Enter your age: ";: Displays the message "Enter your age: " on the console.

  • std::cin >> age;: Reads the integer value entered by the user after the prompt and stores it in the variable age.

  • std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;: Prints a greeting message along with the user's entered name and age on the console.

  • return 0;: Indicates successful execution of the program and returns 0 to the operating system, indicating no errors occurred during execution.