Aligned Columns

Lab C++ Beginner 10 points

Print a table with names left-aligned in 8 characters and numbers right-aligned in 5: apple 12, kiwi 7, banana 130. Use setw, left and right.

Input

None.

Output

Three lines.

Example

Output

apple      12
kiwi        7
banana    130
Sample output available
Editor
C++

Hints

setw applies only to the next item.

left and right stay set until changed.

Solution explanation

Unlike most manipulators, setw resets after one use.

Show solution
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << left << setw(8) << "apple" << right << setw(5) << 12 << "\n";
    cout << left << setw(8) << "kiwi" << right << setw(5) << 7 << "\n";
    cout << left << setw(8) << "banana" << right << setw(5) << 130 << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD