Categories
Uncategorised

Structures – Item Data

/* Copyright (C) 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html /

/ Structures – Item inventory program /

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

struct items
{
int num, price;
char name[50], cat[50];
} item[10];

void disp (items it[10])
{
cout<<endl<<“Displaying items of oil category”<<endl;
for (int i=0; i<10; i++)
if (strcmp(it[i].cat, “Oil”) == 0)
{
cout<<endl<<“Item number : “<<it[i].num<<endl<<“Item name : “;
puts(it[i].name);
cout<<“Item price : “<<it[i].price<<endl;
}
}

void arrange (items it[10])
{
items temp[10];
int min, pos;
for (int i=0; i<=8; i++)
{
min = it[i].price;
pos = i;
for (int j=i+1; j<10; j++)
{
if (it[i].price < min)
{
min = it[i].price;
pos = j;
}
}
temp[i] = it[i];
it[i] = it[pos];
it[pos] = temp[i];
}
cout<<endl<<“Itemdata in ascending order of price”<<endl;
for(int k=0; k<10; k++)
{
cout<<endl<<“Details of item “<<k+1<<endl<<“Item number :
<<it[k].num<<endl<<“Item
name : “;
puts(it[k].name);
cout<<“Item price : “<<it[k].price<<endl<<“Item category : “;
puts(it[k].cat);
}
}

void count (items it[10])
{
intj=0;
for (int i=0; i<10; i++)
if (it[i].price > 500)
j++;
cout<<endl<<“Number of items whose price is more than 500 is “<<j<<endl;
}

void main()
{
clrscr();
cout<<“Item inventory program”<<endl;
for (int i=0; i<10; i++)
{
cout<<endl<<“Enter details for item “<<i+1<<endl
<<“Enter item
number : “;
cin>>item[i].num;
cout<<“Enter item name :”;
gets(item[i].name);
cout<<“Enter item price : “;
cin>>item[i].price;

cout<<“Enter item category : “;

gets(item[i].cat);
cout<<endl;
}
int ch;
cout<<endl<<“Program Menu”<<endl<<“1. Display all items of oil category”
<<endl
<<“2. Arrange items in ascending order according to price”
<<endl<<“3. Count the number of items whose price is more than 500″
<<endl<<“Enter your choice : “;

cin>>ch;
switch (ch)
{
case 1: disp(item);
break;
case 2 : arrange(item);
break;
case 3 : count(item);
break;
default : cout<<“You entered an invalid choice”;
}
cout<<endl;
getch();
}

/ Output */

Item inventory program

Enter details for item 1
Enter item number : 45
Enter item name : The Hitchhiker’s Guide to the Galaxy
Enter item price : 654
Enter item category : Book

Program Menu
1. Display all items of oil category
2. Arrange items in ascending order according to price
3. Count the number of items whose price is more than 500
Enter your choice : 2

Item data in ascending order of price

Details of item 1
Item number : 45
Item name : The Hitchhiker’s Guide to the Galaxy
Item price : 654
Item category : Book

Details of item 2
Item number : 64
Item name : Sunflower
Item price : 554
Item category : Oil

Program Menu
1. Display all items of oil category
2. Arrange items in ascending order according to price
3. Count the number of items whose price is more than 500
Enter your choice : 1

Displaying items of oil category

Details of item 2
Item number : 64
Item name : Sunflower
Item price : 554

Program Menu
1. Display all items of oil category
2. Arrange items in ascending order according to price
3. Count the number of items whose price is more than 500
Enter your choice : 3

Number of items whose price is more than 500 is 4

One reply on “Structures – Item Data”

There was an error at one point, where strcmp was comparing string values to 1. It should have been 0. The error has been corrected.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.