Product Management in C++ Structure only with menu options

//Example of structure with object of arrays and using function
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

struct Product
{
int pid;
char pname[20];
char category[20];
int qty;
float price;
float amount;
};
Product p[100]={
{1,"Pencil","Stationary",100,20,2000},
{2,"Pen","Stationary",200,30,6000},
{3,"Mouse","Electronics",10,300,3000},
{4,"Speakers","Electronics",5,500,2500}
};
int I=4;

void input(Product &p)
{
cout<<"================================ \n";
cout<<"Enter Product ID      : "; cin>>p.pid;
cout<<"Enter Product Name    : "; gets(p.pname);
cout<<"Enter Product Category: "; gets(p.category);
cout<<"Enter Product Qty     : "; cin>>p.qty;
cout<<"Enter Product Price   : "; cin>>p.price;
p.amount=p.price*p.qty;
}

void output(Product &p)
{
cout<<"================================\n";
cout<<"Product ID    : "<<p.pid<<endl;
cout<<"Product Name  : "<<p.pname<<endl;
cout<<"Product Cat.  : "<<p.category<<endl;
cout<<"Product Qty   : "<<p.qty<<endl;
cout<<"Product Price : "<<p.price<<endl;
cout<<"Product Amount: "<<p.amount<<endl;
}

void showall()
{
for(int i=0;i<I;i++)
{
cout<<"================================\n";
cout<<"Product ID    : "<<p[i].pid<<endl;
cout<<"Product Name  : "<<p[i].pname<<endl;
cout<<"Product Cat.  : "<<p[i].category<<endl;
cout<<"Product Qty   : "<<p[i].qty<<endl;
cout<<"Product Price : "<<p[i].price<<endl;
cout<<"Product Amount: "<<p[i].amount<<endl;
}
}

void searchByID()
{
int id,flag=0;
cout<<"Enter the Product ID you want to search : ";
cin>>id;
for(int i=0;i<I;i++)
{
if(id==p[i].pid)
{
output(p[i]);
flag++;
}
}
if(flag==0)
cout<<"The Product ID: "<<id<<" is not in list...\n";
}

void searchByCategory()
{
char cat[20];
int flag=0;
cout<<"Enter the Product Category you want to search : ";
cin>>cat;
for(int i=0;i<I;i++)
{
if(strcmp(cat,p[i].category)==0)
{
output(p[i]);
flag++;
}
}
if(flag==0)
cout<<"The Product category: "<<cat<<" is not in list...\n";
}

void Modify()
{
int id,flag=0,pos;
cout<<"Enter the Product ID you want to Modify: ";
cin>>id;
for(int i=0;i<I;i++)
{
if(id==p[i].pid)
{
cout<<"\n\tThe following ID will be modified:\n";
output(p[i]);
flag++;
pos=i;
}
}
if(flag==0)
cout<<"The Product ID: "<<id<<" is not in list...\n";
else
{
cout<<"\nEnter the new data for the above id : "<<id<<endl;
input(p[pos]);
cout<<"\nThe data has been modified accordingly...\n";
}
}



void main()
{
int ch;
do
{
clrscr();
cout<<"\n\tMenu of Structure\n";
cout<<"==========================================\n";
cout<<"0. Exit\n";
cout<<"1. Add Product\n";
cout<<"2. Display all products\n";
cout<<"3. Search by Product ID\n";
cout<<"4. Search by Category\n";
cout<<"5. Modify Product Details\n";
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: input(p[I]); I++; break;
case 2: showall(); break;
case 3: searchByID(); break;
case 4: searchByCategory(); break;
case 5: Modify(); break;
}
getch();
}while(ch>0);
}



No comments

Post your comments

Powered by Blogger.