Variables

2.2 - Receiving input


The input counterpart to std::cout is std::cin. We use std::cin with the >> operator to receive input for the program.

This line creates an empty std::string variable called name:

std::string name{};

This line takes an input word and set it to name:

std::cin >> name;

Task

Make the program receive name as an input.

  • Define name with std::string name{};.
  • Input name with std::cin >> name;.
#include <iostream> #include <string> int main() { // Create and input name here! std::cout << "Hello, " << name << std::endl; }