Print one line of a price list in fixed columns.
What to build
A line whose columns have fixed widths.
Requirements
- Read an item name and a price.
- Print the name left-aligned in 10 characters.
- Print the price right-aligned in 8 characters with two decimals.
Input
One line: the name and the price.
Output
One line.
Constraints
The name has at most 10 letters. The price is from 0 to 9999.
Example
Input:
Rice 65.5
Output:
Rice 65.50
Why it works
Because setw lasts for one value only, it has to be set for each column. The alignment and precision settings stay in force.
Examples
Example 1 · Input
Rice 65.5
Expected output
Rice 65.50
Example 2 · Input
Oil 180
Expected output
Oil 180.00
Hints
Hint 1
setw must be sent just before each value.
Hint 2
Turn on fixed and setprecision(2) for the price.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string name;
double price;
cin >> name >> price;
cout << left << setw(10) << name;
cout << right << fixed << setprecision(2) << setw(8) << price << endl;
return 0;
}