This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Wednesday, March 15, 2023

Write a program to read a list containing item name, item code, and cost interactively and produce a three column output. Note that the name and code are left-justified with a precision of two digits.

 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;

    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

Explanation:

  1. The program starts by including necessary header files iostream, iomanip, and string.
  2. Three variables are declared, a string variable for name and code, and a double variable for cost.
  3. The user is asked to enter the item name, item code, and cost.
  4. setw() manipulator is used to set the width of each column to be displayed.
  5. left manipulator is used to left-align the output in each column.
  6. setprecision(2) manipulator is used to show only two digits after the decimal point.
  7. fixed manipulator is used to set the number of digits after the decimal point to be constant.
  8. The output is displayed with appropriate formatting.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget