So far, we've only been outputting data. Now let's learn how to get input from users!
We use std::cin
(character input) with the extraction operator >>
to read user input:
int age;
std::cout << "Enter your age: ";
std::cin >> age;
The program will pause and wait for the user to type something and press Enter. The input is then stored in the variable.
Important: Always prompt the user with std::cout
before using std::cin
so they know what to enter!
Declare an integer variable named userNumber
, then use std::cin
to get a number from the user and store it in that variable.
Click "Check Answer" to run your code.