7.Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME CODE COST Turbo C++ 1001 250.95 C Primer 905 95.70 ------------- ------- ---------- ------------- ------- ---------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown.
/*J_4_P_8
Write a program to read a list containing item name, item code and cost
interactively and produce a three-column output as shown below.
NAME CODE COST
=======================
Turbo C++ 1001 250.95
C Primer 905 95.70
------------- ------- -----------------
------------- ------- -----------------
Note that the name and code are left-justified and the cost is right-justified
with a precision of two digits. Trailing zeros are shown.
*/
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
class item
{
char name[40];
int code;
float cost;
public:
void get_data(char *n, int c, float co)
{
strcpy(name, n);
code = c;
cost = co;
}
void display();
};
void item::display()
{
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
cout.setf(ios::showpoint);
cout.setf(ios::left, ios::adjustfield);
cout << setw(40) << name << code;
cout.setf(ios::right, ios::adjustfield);
cout << setw(15) << cost << endl;
}
void main()
{
clrscr();
item a[5];
a[0].get_data("Tarbo C++", 1001, 250.95);
a[1].get_data("C primer", 905, 95.7);
a[2].get_data("algorithm", 1111, 120.5);
a[3].get_data("principle of electronics", 2220, 150.85);
a[4].get_data("solution of balagurusamy", 6666, 145.00);
cout << setw(10) << "name" << setw(34) << "code" << setw(15) << "cost" << endl;
for (int i = 0; i < 60; i++)
cout << "=";
cout << endl;
for (i = 0; i < 5; i++)
a[i].display();
getch();
}
Comments
Post a Comment