Chapter 5: Input & Output (I/O)

Input & Output (I/O) in C++: Talking to the User Like a Pro!

Hello my wonderful student! 👋 Welcome to one of the most exciting and most used parts of C++ — Input and Output (I/O)!

Up to now, we’ve been printing fixed messages like “Hello, World!”. Today we’re going to make programs that talk back to the user:

  • Ask for their name, age, marks, favorite color…
  • Read what they type
  • Print beautiful, nicely formatted output

We’ll cover everything step-by-step, with lots of live examples, common mistakes, and pro tips that even senior developers use daily.

Let’s dive in!

  1. The Three Main I/O Streams

C++ gives us three important streams (think of them as pipes for data):

Stream Purpose When to use Buffered?
std::cout Standard Output (normal printing) Most messages, results, user feedback Yes
std::cin Standard Input (reading from keyboard) Get data from user
std::cerr Standard Error (error messages) Print serious errors/warnings No (immediate)

Quick rule of thumb (what most pros follow):

  • Use cout for normal output
  • Use cerr for error messages (red text in many terminals!)
  • Use cin to read user input

2. Basic Output with std::cout

We’ve already used it — but let’s see the full power.

C++

Newline options — three common ways:

C++

Pro tip: std::endl is slightly slower because it flushes the buffer (forces immediate print). In most cases, just use \n — it’s faster and good enough.

3. Reading Input with std::cin

The extraction operator >> reads data from the user.

C++

Important limitation of >>: It stops at whitespace (space, tab, enter).

So if user types: Webliance Kumar → name will only get “Webliance” → leftover “Kumar” stays in the input buffer → next cin will read it by mistake!

Solution → Use std::getline() for full lines (see below)

4. Reading Full Lines with std::getline()

Best way to read names, addresses, sentences, etc.

C++

Very common mistake & fix:

C++

Fix – eat the leftover newline:

C++

Even safer version (recommended):

C++

(You need to #include <limits> and #include <iostream>)

5. Beautiful & Formatted Output – Manipulators!

C++ has I/O manipulators (from <iomanip>) to make output look professional.

C++

Most useful manipulators:

Manipulator Effect
std::fixed Show decimals (not scientific)
std::scientific Show in scientific notation
std::setprecision(n) Show n digits after decimal (with fixed)
std::setw(n) Set field width to n characters
std::left / std::right Align text left or right in field
std::setfill(‘*’) Fill empty space with ‘*’ (or any char)

6. Using std::cerr for Errors

C++

In many terminals, cerr appears in red — very useful for debugging!

7. Full Practical Example – Mini Student Record Program

C++

Your Mini Homework (Try These!)

  1. Write a program that asks for:
    • First name
    • Last name
    • Age
    • Height in cm (use double) Print a nice summary using setw and fixed.
  2. Create a program that asks for two numbers and prints:
    • Sum
    • Difference
    • Product
    • Quotient (with 4 decimal places)
  3. Make a program that reads a full sentence and prints it in uppercase (hint: you’ll learn string functions soon!).

You’re doing amazing! Now your programs can talk and listen — that’s when they start feeling alive!

Next lesson: Control Structures (if, else, switch, loops) — the real decision-making power!

Any questions? Confused about getline vs cin? Want more formatting examples? Just tell me — your friendly C++ teacher is always here! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *