CPP User Input

C++ Tutorial

💻 C++ User Input (CPP User Input) – Complete Beginner to Interview Guide

User input in C++ means taking data from the user at runtime.
C++ mainly uses cin (from <iostream>) to read input.


1️⃣ Basic User Input Using cin

Example


 

Input

10

Output

10

📌 cin reads input from keyboard.


2️⃣ Taking Multiple Inputs ⭐


Input

5 10

Output

5 10

3️⃣ User Input with Message ⭐


Input

20

Output

Enter age: 20
Age is 20

4️⃣ Input Different Data Types ⭐⭐

Integer

int a;
cin >> a;

Float

float marks;
cin >> marks;

Character

char ch;
cin >> ch;

5️⃣ String Input ⭐⭐

Using cin (single word only)

string name;
cin >> name;

Input:

Sanjit

✔ Reads only one word


Using getline() (full line) ⭐⭐⭐

string name;
getline(cin, name);

✔ Reads full sentence including spaces


6️⃣ cin with getline() Issue ⚠️ (Very Important)

int age;
string name;
cin >> age;
getline(cin, name); // ❌ skipped

✔ Correct Way

cin >> age;
cin.ignore();
getline(cin, name);

7️⃣ User Input in Calculations ⭐⭐


Input

10 5

Output

50

8️⃣ Input Inside Loops ⭐⭐


 

Input

3
5 10 15

Output

5 10 15

9️⃣ Common User Input Errors ❌

❌ Using cout instead of cin
❌ Forgetting >>
❌ Wrong data type
❌ Not handling getline() properly


📌 Interview Questions (CPP User Input)

Q1. Which operator is used with cin?
👉 >> (extraction operator)

Q2. Difference between cin and getline()?
👉 cin reads one word, getline() reads full line

Q3. What happens if wrong data type is entered?
👉 Input fails (stream error)


✅ Summary

cin is used for user input
>> extracts input
✔ Multiple inputs allowed
getline() for full strings
✔ Handle newline issues carefully

You may also like...