Definition
📌 Input & Output
C++ introduces stream-based I/O: cout for output and cin for input, both from <iostream>.
Syntax
syntax
cout << value;
cin >> variable;Example
cpp
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0;
}Output
outputEnter your age: 20
You are 20 years old.
Important Points
cout << a << b;chains multiple outputs on one line.cin >> a >> b;can read multiple values in one line, separated by whitespace.- No
&is needed before variables withcin— it's handled automatically by references. endlprints a newline and flushes the output buffer.
Common Mistakes
- Forgetting
using namespace std;and then writing barecout(needsstd::coutinstead). - Mixing up
<<and>>. - Reading a string with spaces using
cin >>, which stops at the first space — usegetline()instead.
Interview Tip
🎯 Before your interview
Know when to use
getline(cin, str) instead of cin >> str — for input that includes spaces.
