We use cin to get information from the user while the program is running. It helps the program learn what the user wants.
When you want the user to type their name.
When you need numbers from the user to do math.
When you want to ask the user a question and get their answer.
When you want to make the program interactive.
When you want to read multiple pieces of information one after another.
Syntax
C++
std::cin >> variable;
std::cin reads input from the keyboard.
The >> operator sends the input into the variable.
Examples
This reads an integer number from the user and stores it in age.
C++
int age;
std::cin >> age;
This reads a word (no spaces) from the user and stores it in name.
C++
std::string name;
std::cin >> name;
This reads a decimal number from the user and stores it in price.
C++
double price;
std::cin >> price;
Sample Program
This program asks the user to type their name and age. Then it says hello using the information typed.
C++
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return0;
}
OutputSuccess
Important Notes
cin stops reading at the first space, so it reads only one word for strings.
To read a full line with spaces, use std::getline(std::cin, variable); instead.
If the user types the wrong type (like letters when a number is expected), the program may not work as expected.
Summary
cin is used to get input from the user.
Use >> to send input into variables.
It works well for simple inputs like numbers and single words.
Practice
(1/5)
1. What does cin do in a C++ program?
easy
A. It prints output to the screen.
B. It reads input from the user.
C. It declares a variable.
D. It creates a loop.
Solution
Step 1: Understand the role of cin
cin is used to get input from the user in C++ programs.
Step 2: Differentiate from other commands
Unlike cout which outputs data, cin reads data from the keyboard.
Final Answer:
It reads input from the user. -> Option B
Quick Check:
cin = input [OK]
Hint: Remember: cin means input from user [OK]
Common Mistakes:
Confusing cin with cout
Thinking cin outputs data
Assuming cin declares variables
2. Which of the following is the correct syntax to read an integer variable age using cin?
easy
A. cin >> age;
B. cin >> &age;
C. cin >> "age";
D. cin << age;
Solution
Step 1: Recall the operator for input
cin uses the extraction operator >> to read input into variables.
Step 2: Check each option
cin >> age; uses cin >> age; which is correct syntax to read an integer variable.
Final Answer:
cin >> age; -> Option A
Quick Check:
Use >> with cin [OK]
Hint: Use >> operator with cin to read input [OK]
Common Mistakes:
Using << instead of >> with cin
Trying to input variable name as string
Using address operator & incorrectly
3. What will be the output of this code if the user inputs 42?
int num;
std::cin >> num;
std::cout << num + 10;
medium
A. num + 10
B. 4210
C. 52
D. Syntax error
Solution
Step 1: Understand input and variable assignment
User inputs 42, which is stored in variable num.
Step 2: Calculate output expression
num + 10 equals 42 + 10 = 52, which is printed.
Final Answer:
52 -> Option C
Quick Check:
42 + 10 = 52 [OK]
Hint: Add input value to 10 and print [OK]
Common Mistakes:
Treating input as string and concatenating
Expecting variable name printed
Assuming syntax error without checking code
4. Identify the error in this code snippet:
int x;
std::cin >> x
std::cout << x;
medium
A. Variable x not declared
B. Wrong operator used with cin
C. No error
D. Missing semicolon after std::cin >> x
Solution
Step 1: Check syntax line by line
The line std::cin >> x is missing a semicolon at the end.
Step 2: Confirm other parts
Variable x is declared, and operators are correct.
Final Answer:
Missing semicolon after std::cin >> x -> Option D
Quick Check:
Every statement needs a semicolon [OK]
Hint: Check for missing semicolons after input lines [OK]
Common Mistakes:
Forgetting semicolon after input statement
Confusing input operator >> with <<
Assuming variable not declared
5. You want to read two integers a and b from the user and print their sum. Which code snippet correctly does this?
hard
A. int a, b;
std::cin >> a >> b;
std::cout << a + b;
B. int a, b;
std::cin << a << b;
std::cout << a + b;
C. int a, b;
std::cin >> a, b;
std::cout << a + b;
D. int a, b;
std::cin >> (a + b);
std::cout << a + b;
Solution
Step 1: Understand how to read multiple inputs
Use std::cin >> a >> b; to read two integers from the user.
Step 2: Check each option for correctness
Only the snippet with chained >> operators (std::cin >> a >> b;) is correct. Others use the output operator <<, commas instead of chaining, or try to read into an expression (a + b).
Final Answer:
int a, b;
std::cin >> a >> b;
std::cout << a + b; -> Option A
Quick Check:
Use chained >> operators for multiple inputs [OK]
Hint: Chain >> operators to read multiple variables [OK]
Common Mistakes:
Using << instead of >> with cin
Separating inputs with commas instead of >>
Trying to input expressions instead of variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.