Learn how to use C++ cin for standard input with beginner-friendly examples. Understand cin >> syntax, multiple inputs, user input, and simple C++ programs for reading integers, floats, chars, and strings.
In C++, standard input is used to take data from the user through the keyboard.
The standard input object in C++ is:
cin
cin is provided by the <iostream> library.
We commonly use cin to take:
cinThe basic syntax is:
cin >> variable;
cin → standard input object.>> → extraction operator, which takes data from the keyboard.variable → the variable where the input is stored.Think of it as:
Keyboard → cin → Variable
int age;
cin >> age;
If the user enters:
20
the value 20 is stored in the variable age.
We can take multiple values using a single cin statement.
cin >> a >> b >> c;
For example:
int a, b, c;
cin >> a >> b >> c;
If the user enters:
10 20 30
then:
a = 10
b = 20
c = 30
The user can enter the values separated by spaces or by pressing Enter.
For example, both are valid:
10 20 30
or:
10
20
30
Write a C++ program that asks the user to enter an integer and displays the entered value.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
cout << "You entered: " << num;
return 0;
}
Enter an integer: 25
You entered: 25
int num;
Creates an integer variable named num.
cin >> num;
Takes an integer from the user and stores it in num.
cout << "You entered: " << num;
Displays the value stored in num.
Write a C++ program that takes two integers from the user and displays their sum.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b;
return 0;
}
Enter two numbers: 10 20
Sum = 30
cin >> a >> b;
takes two values from the user:
abThen:
a + b
calculates their sum.
Write a C++ program that reads a floating-point number from the user and displays it.
#include <iostream>
using namespace std;
int main() {
float value;
cout << "Enter a floating-point value: ";
cin >> value;
cout << "You entered: " << value;
return 0;
}
Enter a floating-point value: 12.5
You entered: 12.5
Use float when you want to store numbers that may contain a decimal part.
For example:
12.5
7.25
100.75
Write a C++ program that asks the user to enter a single character and displays it.
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
cout << "You entered: " << ch;
return 0;
}
Enter a character: A
You entered: A
A char variable stores a single character.
Examples:
A
B
7
$
Write a C++ program that reads a single word from the user and displays a welcome message.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Welcome, " << name << "!";
return 0;
}
Enter your name: Ali
Welcome, Ali!
string name;
creates a variable named name that can store text.
cin >> name;
takes a single word from the user.
cin >> stops reading when it encounters a space.
For example, if the user enters:
Muhammad Yasir
with:
cin >> name;
only:
Muhammad
will be stored in name.
To read a complete line containing spaces, we use getline():
getline(cin, name);
We will study getline() separately.
cin with Different Data TypesThe type of variable determines what kind of input we expect.
| Data Type | Example Input | Example Variable |
|---|---|---|
int |
25 |
int age; |
float |
12.5 |
float marks; |
double |
125.75 |
double price; |
char |
A |
char grade; |
string |
Ali |
string name; |
Example:
int age;
float marks;
char grade;
string name;
cin >> age;
cin >> marks;
cin >> grade;
cin >> name;
cin and cout TogetherIn most beginner programs, cin and cout are used together.
cout → displays a message or result.cin → takes input from the user.#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
return 0;
}
Think of the process as:
User
│
│ enters data
▼
cin
│
▼
Variable
│
▼
cout
│
▼
Screen
std::cinWe can also use cin without:
using namespace std;
In that case, we write:
std::cin >> age;
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Your age is: " << age;
return 0;
}
Here, std::cin means that cin belongs to the standard (std) namespace.
| Component | Purpose |
|---|---|
#include <iostream> |
Provides input/output features |
cin |
Takes input from the user |
>> |
Extraction operator |
cout |
Displays output on the screen |
<< |
Insertion operator |
int |
Stores integer values |
float |
Stores decimal values |
char |
Stores a single character |
string |
Stores text |
cinUsed to take input from the user.
cin >> age;
>>Called the extraction operator.
cin >> variable;
It extracts data from the input stream and stores it in the variable.
cin >> a >> b >> c;
cout vs cincin → Input → User → Program
cout → Output → Program → Screen
Write a C++ program that asks the user to enter their age and displays:
Your age is: 20
Write a C++ program that takes two integers from the user and displays their:
Write a C++ program that takes marks of three subjects and displays the total marks.
Write a C++ program that takes the length and width of a rectangle and calculates its area.
Formula:
Area = Length × Width
Write a C++ program that takes the following information from the user:
Then display all the information using cout.
cin → Takes input from the user
cout → Displays output on the screen
>> → Extraction operator
<< → Insertion operator
cin is used for input, while cout is used for output.