Back to Curriculum

8. User Input

Getting Input from Users

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!

Your Task

Declare an integer variable named userNumber, then use std::cin to get a number from the user and store it in that variable.

🎉

Lesson Complete!

Excellent! You now know how to get input from users, making your programs interactive.

Continue to Lesson 9: Useful Math Related Functions
main.cpp
Output
Click "Check Answer" to run your code.