Kali ini ali ini Admin @feidodol akan membuat Program yang berfungsi mengolah data, adding data dan deleting data. Ikuti terus yaaa....
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<string.h>
using namespace std;
struct TItem
{
int id;
char name[100];
long qty;
};
TItem item[1000]; //Array struct
int item_count = 0;
//function declaration
int search(int _id);
void add(int _id,char _name[],long _qty);
void del(int _id);
void menu_add();
void menu_del();
void menu_view();
int main()
{
int choice=0;
do{
cout<<"=== ITEM MANAGEMENT ==="<<endl;
cout<<"1. Add item"<<endl;
cout<<"2. Delete item"<<endl;
cout<<"3. View item"<<endl;
cout<<"4.Exit"<<endl<<endl;
do{
cout<<"Input? ";
cin>>choice;
}
while((choice<1) || (choice>4));
cout<<endl;
switch(choice)
{
case 1 : menu_add(); break;
case 2 : menu_del(); break;
case 3 : menu_view(); break;
case 4 : break;
}
cout<<endl;
}
while(choice != 4);
return 0;
}
int search(int _id)
{
for(int i=0; i<item_count; i++)
{
if(item[i].id == _id) return(i);
}
return (-1); //item not found
}
void add(int _id,char _name[], long _qty)
{
item[item_count].id = _id;
strcpy(item[item_count].name, _name);
item[item_count].qty = _qty;
item_count = item_count +1;
}
void del(int _id)
{
int idx=search(_id);
for(int i=idx+1; i<item_count; i++)
{
item[i-1]=item[i];
}
item_count = item_count - 1;
}
void menu_add()
{
int _id;
char _name[100];
long _qty;
cout<< "ADD NEW ITEM"<<endl;
cout<<"ITEM ID [0-1000] = ";
cin>> _id;
getchar();
cout<<"Item Name = ";
cin.getline(_name,sizeof(_name));
cout<<"Quantity = ";
cin>>_qty;
add(_id, _name, _qty);
cout<<"Item added."<<endl;
}
void menu_del()
{
int _id;
cout<<"DELETE ITEM"<<endl;
cout<<"Item ID = ";
cin>> _id;
int idx = search(_id);
if(idx > -1)
{
char answer;
cout<<"Item found. Are you sure [Y/N]? ";
cin>>answer;
if(toupper(answer) == 'Y')
{
del(_id);
cout<<"Deleted."<<endl;
}
else cout<<"Aborted."<<endl;
}
else cout<<"Item not found."<<endl;
}
void menu_view()
{
cout<<"ITEM LIST "<<endl;
cout<<setiosflags(ios::left);
cout<<setw(5) << "ID"<<setw(30)<< "NAME"<<setw(10)<< "QTY"<<endl;
for(int i= 0; i<item_count; i++)
{
TItem P = item[i];
cout<<setw(5)<< P.id<<setw(30)<<P.name<<setw(10)<<P.qty<<endl;
}
}
Inilah Hasilnya....
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<string.h>
using namespace std;
struct TItem
{
int id;
char name[100];
long qty;
};
TItem item[1000]; //Array struct
int item_count = 0;
//function declaration
int search(int _id);
void add(int _id,char _name[],long _qty);
void del(int _id);
void menu_add();
void menu_del();
void menu_view();
int main()
{
int choice=0;
do{
cout<<"=== ITEM MANAGEMENT ==="<<endl;
cout<<"1. Add item"<<endl;
cout<<"2. Delete item"<<endl;
cout<<"3. View item"<<endl;
cout<<"4.Exit"<<endl<<endl;
do{
cout<<"Input? ";
cin>>choice;
}
while((choice<1) || (choice>4));
cout<<endl;
switch(choice)
{
case 1 : menu_add(); break;
case 2 : menu_del(); break;
case 3 : menu_view(); break;
case 4 : break;
}
cout<<endl;
}
while(choice != 4);
return 0;
}
int search(int _id)
{
for(int i=0; i<item_count; i++)
{
if(item[i].id == _id) return(i);
}
return (-1); //item not found
}
void add(int _id,char _name[], long _qty)
{
item[item_count].id = _id;
strcpy(item[item_count].name, _name);
item[item_count].qty = _qty;
item_count = item_count +1;
}
void del(int _id)
{
int idx=search(_id);
for(int i=idx+1; i<item_count; i++)
{
item[i-1]=item[i];
}
item_count = item_count - 1;
}
void menu_add()
{
int _id;
char _name[100];
long _qty;
cout<< "ADD NEW ITEM"<<endl;
cout<<"ITEM ID [0-1000] = ";
cin>> _id;
getchar();
cout<<"Item Name = ";
cin.getline(_name,sizeof(_name));
cout<<"Quantity = ";
cin>>_qty;
add(_id, _name, _qty);
cout<<"Item added."<<endl;
}
void menu_del()
{
int _id;
cout<<"DELETE ITEM"<<endl;
cout<<"Item ID = ";
cin>> _id;
int idx = search(_id);
if(idx > -1)
{
char answer;
cout<<"Item found. Are you sure [Y/N]? ";
cin>>answer;
if(toupper(answer) == 'Y')
{
del(_id);
cout<<"Deleted."<<endl;
}
else cout<<"Aborted."<<endl;
}
else cout<<"Item not found."<<endl;
}
void menu_view()
{
cout<<"ITEM LIST "<<endl;
cout<<setiosflags(ios::left);
cout<<setw(5) << "ID"<<setw(30)<< "NAME"<<setw(10)<< "QTY"<<endl;
for(int i= 0; i<item_count; i++)
{
TItem P = item[i];
cout<<setw(5)<< P.id<<setw(30)<<P.name<<setw(10)<<P.qty<<endl;
}
}
Inilah Hasilnya....
Komentar
Posting Komentar