apecifying fielf size with width() in C++.

 



#include <iostream>
using namespace std;
int main()
{
    int item[4] = {105834};
    int cost[4] = {2034234};

    cout.width(5);
    cout << "ITEMS";
    cout.width(8);
    cout << "COST";
    cout.width(15);
    cout << "TOTAL PRICE\n";
    int sum = 0;
    for (int i = 0i < 4i++)
    {
        cout.width(5);
        cout << item[i];
        cout.width(8);
        cout << cost[i];
        int value = item[i] * cost[i];
        cout.width(14);
        cout << value << endl;

        sum = sum + value;
    }
    cout << "GRAND TOTAL = " << sum;
}

output:
ITEMS COST TOTAL PRICE 10 20 200 5 34 170 8 23 184 34 4 136 GRAND TOTAL = 690

Comments

Popular Post

Write the definition for a class called complex that has floating point data members for storing real and imaginary parts. The class has the following member functions: void set(float, float) to set the specified value in object void disp() to display complex number object complex sum(complex) to sum two complex numbers & return complex number 1. Write the definitions for each of the above member functions. 2. Write main function to create three complex number objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all complex numbers.