C++ User Input
C++ User Input
cin is a predefined variable that reads data from the keyboard with the extraction operator ( >> ).
In the following example, the user can input a number, which is stored in the variable x . Then we
print the value of x :
Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
Run example »
Good To Know
cout is pronounced "see-out". Used for output, and uses the insertion operator ( << )
cin is pronounced "see-in". Used for input, and uses the extraction operator ( >> )
Tutorials Exercises
Creating a Simple Calculator
Services Log in
Example
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
Run example »
C++ Exercises
Exercise:
Use the correct keyword to get user input, stored in the variable x :
intTutorials
x;
Exercises Services Log in
HOW cout
TO W3.CSS
<< "TypeC C++ C# "; BOOTSTRAP
a number: REACT MYSQL JQUERY EX
>> ;
Submit Answer »
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
COLOR PICKER
Tutorials Exercises Services Log in
FORUM ABOUT CLASSROOM
Tutorials Exercises Services Log in
W3Schools is optimized for learning and training. Examples might be simplified to improve reading
and learning.
HOW TO W3.CSS C
Tutorials, references, C++
and C#are constantly
examples BOOTSTRAP REACT
reviewed to avoid errors, but MYSQL JQUERY
we cannot warrant full EX
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie
and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.