Input and output in depth

Introduction

Real input is messy. Names have spaces, and files have an unknown number of lines. Output has to be readable, too. This lesson covers the small set of tools that handle these everyday needs.

How it works

Whole lines. cin >> s stops at the first space. getline(cin, s) reads up to the end of the line. Mixing them has a famous trap: cin >> age leaves the newline waiting, so the next getline reads an empty line. cin.ignore() skips that one character.

#include <iostream>
#include <string>
using namespace std;

int main() {
    int age;
    string name;
    cin >> age;
    cin.ignore();
    getline(cin, name);
    cout << name << " is " << age << endl;
    return 0;
}

Input used: 20 ⏎ Mitu Akter

Open in compiler

Reading until the end. A read like cin >> x can be used as a condition. It is true if it worked, and false when there is no more input or the input does not match:

int x, total = 0;
while (cin >> x) {
    total += x;
}

Formatting. The header <iomanip> adds manipulators, which you send to the stream:

ManipulatorEffectLasts
fixeduse a fixed number of decimalsuntil changed
setprecision(2)two decimals, with fixeduntil changed
setw(8)a width of 8the next value only
left, rightalignment within the widthuntil changed
boolalphaprint true and false as wordsuntil changed

Syntax

getline(cin, line);
while (cin >> x) { ... }
cout << fixed << setprecision(2) << x;
cout << left << setw(10) << name;

Example

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {
    string item;
    int qty;
    double price;
    cin >> item >> qty >> price;
    cout << left << setw(8) << item;
    cout << right << setw(4) << qty;
    cout << fixed << setprecision(2) << setw(9) << qty * price << endl;
    return 0;
}

Input used: Rice 2 65.5

Open in compiler

Line by line

  1. Three values are read from one line.
  2. left << setw(8) prints the item in 8 characters, aligned left.
  3. right << setw(4) prints the quantity in 4 characters, aligned right.
  4. fixed << setprecision(2) turns on two decimals.
  5. setw(9) gives the cost a width of 9.

Expected result

With this input:

Rice 2 65.5

it prints:

Rice       2   131.00

In the real world

Command-line tools process files by reading until the end of their input, so that they can work on anything from one line to millions. Formatted columns make logs and reports easy to scan.

Common mistakes

  • Calling getline straight after cin >>, and getting an empty line.
  • Expecting setw to stay in force. It applies only to the next value.
  • Forgetting fixed, so that setprecision counts all the digits, not only the decimals.
  • Forgetting #include <iomanip>.

Best practices

  • Use getline whenever a value can contain spaces.
  • Use while (cin >> x) rather than counting the input yourself when you can.
  • Set up fixed and setprecision once, near the start.

Summary

getline reads whole lines, and cin.ignore() skips a leftover newline. A read can be used as a loop condition. <iomanip> controls decimals and widths, and setw affects only the next value.

Quick check

1. What reads a whole line, including its spaces?
2. When does while (cin >> x) stop?
3. Which setting lasts only for the next value?
4. Why might getline read an empty line after cin >> n?

Key takeaways

  • cin >> stops at spaces, and getline does not.
  • while (cin >> x) reads until the input ends.
  • setw applies to one value only, and setprecision stays in force.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.