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
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:
| Manipulator | Effect | Lasts |
|---|---|---|
fixed | use a fixed number of decimals | until changed |
setprecision(2) | two decimals, with fixed | until changed |
setw(8) | a width of 8 | the next value only |
left, right | alignment within the width | until changed |
boolalpha | print true and false as words | until 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
Line by line
- Three values are read from one line.
left << setw(8)prints the item in 8 characters, aligned left.right << setw(4)prints the quantity in 4 characters, aligned right.fixed << setprecision(2)turns on two decimals.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
getlinestraight aftercin >>, and getting an empty line. - Expecting
setwto stay in force. It applies only to the next value. - Forgetting
fixed, so thatsetprecisioncounts all the digits, not only the decimals. - Forgetting
#include <iomanip>.
Best practices
- Use
getlinewhenever a value can contain spaces. - Use
while (cin >> x)rather than counting the input yourself when you can. - Set up
fixedandsetprecisiononce, 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
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.