C++ 
				- C++ Introduction
 - C++ Getting Started
 - C++ Syntax
 - C++ Output (Print Text)
 - C++ Comments
 - C++ Variables
 - C++ Declare Multiple Variables
 - C++ Identifiers
 - C++ User Input
 - C++ Data Types
 - C++ Operators
 - C++ Strings
 - C++ Math
 - C++ Booleans
 - C++ Conditions
 - C++ Switch
 - C++ While Loop
 - C++ For Loop
 - C++ Break And Continue
 - C++ Arrays
 - C++ References
 - C++ Pointers
 - C++ Functions
 - C++ Function Overloading
 - C++ OOP
 - C++ Classes And Objects
 - C++ Class Methods
 - C++ Constructors
 - C++ Access Specifiers
 - C++ Encapsulation
 - C++ Inheritance
 - C++ Multilevel Inheritance
 - C++ Multiple Inheritance
 - C++ Inheritance Access
 - C++ Polymorphism
 - C++ Files
 - C++ Exceptions
 - C++ How To Add Two Numbers
 
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 (>>).
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
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
Practice Excercise Practice now
Creating A Simple Calculator
In this example, the user must input two numbers. Then we print the sum by calculating (adding) the two numbers:
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;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP