Input and Output — Talking to Your Computer
Master cin and cout — the walkie-talkies between you and your program!
Open interactive version (quiz + challenge)Real-world analogy
What is it?
Input and Output (I/O) is how your program communicates with the outside world. Input means receiving data (from the user, a file, or an online judge), and output means sending results back. In C++, we use cin for input and cout for output. In competitive programming, getting I/O right is absolutely essential — your code must read the exact input format and produce the exact output format that the problem specifies.
Real-world relevance
Every program you use involves Input and Output. When you type in a search box (input) and see results (output) — that is I/O. When you tap a key on your phone (input) and see a letter appear (output) — that is I/O. When you scan a barcode at a store (input) and the price shows up (output) — that is I/O. Programming is fundamentally about taking input, processing it, and producing output!
Key points
- cout — Your Program Speaks — cout (pronounced 'see-out') is how your program prints things to the screen. You use the << operator (called the insertion operator) to send data to cout. You can print text in double quotes, numbers, variables, or even calculations. Example: cout << 42 << endl; prints the number 42. Think of << as arrows pointing toward cout — you are pushing data out to the screen.
- cin — Your Program Listens — cin (pronounced 'see-in') is how your program reads input. You use the >> operator (called the extraction operator) to pull data from cin into a variable. Example: cin >> n; reads a value and stores it in n. Think of >> as arrows pointing toward your variable — you are pulling data from the keyboard (or from the judge) into your box.
- Reading Multiple Values — You can read multiple values in one line: 'cin >> a >> b >> c;' reads three values one after another. The values can be separated by spaces or newlines — cin does not care which. So if the input is '3 5 7' or if 3, 5, and 7 are on separate lines, cin >> a >> b >> c reads all three correctly.
- Printing Multiple Things — You can chain multiple items in one cout statement: 'cout << "Score: " << score << " out of " << total << endl;' prints them all in order. You can mix text (in quotes), variables, and expressions. The items get printed left to right with no automatic spaces between them — you must include spaces yourself inside the text.
- endl vs the Newline Character — Both endl and the newline character '\n' move to a new line. However, endl also 'flushes the buffer' (forces output to appear immediately), which makes it slightly slower. In CP, when speed matters, use '\n' instead of endl. For most beginner problems either works fine. Example: cout << answer << '\n'; is faster than cout << answer << endl;
- Fast I/O — The Speed Trick — In CP, slow input/output can cause TLE on problems with lots of data. The fix is two magic lines at the start of main(): 'ios_base::sync_with_stdio(false);' and 'cin.tie(NULL);' — these make cin and cout much faster. Add them to EVERY CP solution! After adding these, use '\n' instead of endl for best performance.
- Reading Until End of Input — Some problems do not tell you how many values to read — they just give you data until the input ends. You can handle this with 'while (cin >> x)' which keeps reading values into x until there is no more input. This is a common pattern in CP for problems that say 'read until end of file' or 'process all inputs.'
- Reading Full Lines with getline — cin >> stops reading at spaces, so 'cin >> name;' for input 'John Doe' only reads 'John'. To read an entire line including spaces, use: getline(cin, name); This reads everything until the newline. Warning: if you mix cin >> and getline(), you may need to add cin.ignore(); before getline to skip the leftover newline character.
- Formatting Output — Fixed Decimal Places — Some problems ask you to print a decimal number with exactly K digits after the decimal point. Use: cout << fixed << setprecision(K) << value << endl; For example, 'cout << fixed << setprecision(2) << 3.14159;' prints '3.14'. The 'fixed' part prevents scientific notation, and setprecision(2) means 2 decimal places.
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
// Fast I/O — always include this in CP!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Reading and printing integers
int a, b;
cin >> a >> b;
cout << a + b << "\n";
// Reading a string
string name;
cin >> name;
cout << "Hello, " << name << "\n";
// Printing with fixed decimal places
double pi = 3.14159265;
cout << fixed << setprecision(4) << pi << "\n";
// Output: 3.1416
// Reading multiple values in a loop
int n;
cin >> n;
int sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
sum += x;
}
cout << "Sum: " << sum << "\n";
return 0;
}Line-by-line walkthrough
- 1. #include and using namespace std; — our standard CP starter lines. These give us everything we need.
- 2. ios_base::sync_with_stdio(false); — this disconnects C++ streams from C streams, making cin and cout much faster. Always add this in CP!
- 3. cin.tie(NULL); — this unties cin from cout, meaning cout does not flush before every cin read. Another speed boost for CP.
- 4. int a, b; — we declare two integer variables to hold our input values.
- 5. cin >> a >> b; — we read two numbers from input. If the judge sends '3 5', then a becomes 3 and b becomes 5. The >> reads values separated by any whitespace (spaces, tabs, newlines).
- 6. cout << a + b << "\n"; — we print the sum and then a newline. We use \n (newline character) instead of endl because it is faster. The expression a + b is calculated first, then the result is printed.
- 7. string name; cin >> name; — we declare a string variable and read one word of input into it. Remember, cin >> stops at spaces.
- 8. cout << fixed << setprecision(4) << pi << "\n"; — 'fixed' means do not use scientific notation. setprecision(4) means show exactly 4 digits after the decimal point. So 3.14159265 becomes 3.1416 (it rounds!).
- 9. int n; cin >> n; — a very common CP pattern: first read how many values are coming, then read that many values in a loop.
- 10. for (int i = 0; i > x; sum += x; } — we loop n times. Each time, we read a number and add it to our running total. The 'sum += x' is shorthand for 'sum = sum + x'.
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string fullName;
getline(cin, fullName);
cout << "There are " << n << " students." << "\n";
cout << "Teacher: " << fullName << "\n";
return 0;
}Need a hint?
Show answer
Explain like I'm 5
Fun fact
Hands-on challenge
More resources
- Codeforces — Practice I/O Problems (Rating 800) (Codeforces)
- CP-Algorithms — Competitive Programming Techniques (CP-Algorithms)
- C++ Input/Output for Competitive Programming (YouTube)