Lesson 5 of 50 beginner

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

cin and cout are like walkie-talkies. cout is when the computer talks to you — it sends a message to your screen. cin is when you talk to the computer — you type something, and the computer hears it. In competitive programming, the online judge is on the other end of the walkie-talkie: it sends your program input through cin, and listens for your answer through cout!

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

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. 1. #include and using namespace std; — our standard CP starter lines. These give us everything we need.
  2. 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. 3. cin.tie(NULL); — this unties cin from cout, meaning cout does not flush before every cin read. Another speed boost for CP.
  4. 4. int a, b; — we declare two integer variables to hold our input values.
  5. 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. 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. 7. string name; cin >> name; — we declare a string variable and read one word of input into it. Remember, cin >> stops at spaces.
  8. 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. 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. 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?
When you use cin >> n, it reads the number but leaves the newline character in the input buffer. What happens when getline runs next and finds that leftover newline?
Show answer
The bug is a classic cin/getline mixing issue! After 'cin >> n;' reads the number, the newline character (from pressing Enter) is still in the input buffer. Then 'getline(cin, fullName);' immediately reads that leftover newline and thinks it got an empty line. The fix is to add 'cin.ignore();' between cin >> n and getline(cin, fullName) to skip that leftover newline character.

Explain like I'm 5

Imagine you and a friend are talking through walkie-talkies. When your friend presses the button and talks, you hear them through your walkie-talkie — that is like cout (the computer sending a message to you). When you press the button and talk back, your friend hears you — that is like cin (you sending information to the computer). In CP, the online judge is your walkie-talkie friend: it sends you the problem input, and it listens for your answer!

Fun fact

The two magic lines for fast I/O (ios_base::sync_with_stdio(false) and cin.tie(NULL)) can make your program’s input/output up to 10 times faster! This speed boost has been the difference between TLE and AC for countless competitive programmers. Some competitive programmers even type these lines so often they can do it in under 2 seconds from muscle memory. Many top coders have these in a pre-saved template that they copy before every contest!

Hands-on challenge

Solve this classic CP problem: Read an integer N, then read N numbers. Print the sum and the average (with exactly 2 decimal places) of those numbers. Use fast I/O. Test it with this input: first line is 5, second line is 10 20 30 40 50. The sum should be 150 and the average should be 30.00. Try it on ideone.com!

More resources

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero