C++ User Input C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ User Input
C++ User Input
You have already learned that cout
is used to output (print) values. Now we will use cin
to get user input.
cin
is a predefined variable that reads data from the keyboard with the extraction operator (>>
).
Example
#include <iostream>
using namespace std;int main() {
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;
return 0;
}
Output:
Type a number: [Type number with your keyboard]
OUTPUT: [YOUR TYPED NUMBER]
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 (>>
)