Wednesday, June 15, 2011

Make a simple program C++ with struct

This exmple program made with Turbo C++.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#define n 5

main()
{
struct
{
char name[4],code;
int price,quant,subprice;
}store[6];


double total;
int i;


clrscr();
total=0;

for (i=1;i<=n;++i)
{
clrscr();
cout<<"-------------------------------------------------------------"<<endl;
cout<<"                BANI MART "<<endl;
cout<<"                CHEAP.COM   "<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Enter Code    : ";
cin>>store[i].code;
cout<<"Quantity      : ";
cin>>store[i].quant;
cout<<"------------------------"<<endl;
cout<<endl;
 }

clrscr();
cout<<"----------------------------------------------------------------"<<endl;
cout<<"                BANI MART "<<endl;
cout<<"                MURAH.COM   "<<endl;
cout<<"----------------------------------------------------------------"<<endl;

cout<<"----------------------------------------------------------------"<<endl;
cout<<"No.  Code    Name     Price   Quantity  Subtotal "<<endl;
cout<<"----------------------------------------------------------------"<<endl;
for (i=1;i<=n;++i)
{
switch(store[i].code)
{
case 'A':
{
strcpy(store[i].name,"Book");
store[i].price=1000;
}
break;
case 'B':
{
strcpy(store[i].name,"Pencil");
store[i].price=500;
}
break;
case 'C':
{
strcpy(store[i].name,"Bag");
store[i].price=5000;
}
break;
default:
{
strcpy(store[i].name,"Wrong Code");
store[i].price=0;
}
}
cout<<setiosflags(ios::right)<<setw(1)<<i<<" ";
cout<<setiosflags(ios::right)<<setw(4)<<store[i].code;
cout<<setiosflags(ios::right)<<setw(10)<<store[i].name;
cout<<setiosflags(ios::right)<<setw(8)<<"$ "<<store[i].price;
cout<<setiosflags(ios::right)<<setw(6)<<store[i].quant;
store[i].subprice=(store[i].price*store[i].quant);
cout<<setiosflags(ios::right)<<setw(7)<<"$ " <<store[i].subprice;
cout<<endl;
cout<<"----------------------------------------------------------------"<<endl;
total+=store[i].subprice;
}
cout<<"Total Price                            $ "<<total<<endl;
cout<<"----------------------------------------------------------------"<<endl;

getch();
return 0;
}

Input display.










And output display












Thanks for your visit...

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home