Here's a program to read a list containing item name, item code, and cost interactively and produce a three column output:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string name, code;
double cost;
#include <iomanip>
#include <string>
using namespace std;
int main() {
string name, code;
double cost;
cout << "Enter item name, code, and cost: " << endl;
cin >> name >> code >> cost;
cout << setw(20) << left << "Item Name" << setw(10) << left << "Item Code" << setw(10) << left << "Cost" << endl;
cout << setw(20) << left << name << setw(10) << left << code << setprecision(2) << fixed << cost << endl;
return 0;
}
Example output:
Enter item name, code, and cost:
Shirt SH-01 25.99
Item Name Item Code Cost
Shirt SH-01 25.99
Shirt SH-01 25.99
Item Name Item Code Cost
Shirt SH-01 25.99
Explanation:
- The program starts by including necessary header files iostream, iomanip, and string.
- Three variables are declared, a string variable for name and code, and a double variable for cost.
- The user is asked to enter the item name, item code, and cost.
- setw() manipulator is used to set the width of each column to be displayed.
- left manipulator is used to left-align the output in each column.
- setprecision(2) manipulator is used to show only two digits after the decimal point.
- fixed manipulator is used to set the number of digits after the decimal point to be constant.
- The output is displayed with appropriate formatting.
No comments:
Post a Comment
If you have any doubts, please let me know