0% found this document useful (0 votes)
10 views

no_6[1]

The document contains a C++ program for managing a collection of items, allowing users to insert, display, search, sort, and delete items based on their attributes. It defines an 'item' class with overloaded operators for comparison and sorting, and utilizes a vector to store items. The main function presents a menu for user interaction and executes corresponding actions based on user choices.

Uploaded by

Vaibhav Hoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

no_6[1]

The document contains a C++ program for managing a collection of items, allowing users to insert, display, search, sort, and delete items based on their attributes. It defines an 'item' class with overloaded operators for comparison and sorting, and utilizes a vector to store items. The main function presents a menu for user interaction and executes corresponding actions based on user choices.

Uploaded by

Vaibhav Hoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

// Assignment No: 6

// Name: Vaibbhav Hokr

// Roll No: COSA69

// Division: A

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

class item {

public:

char name[10];

int quantity;

int cost;

int code;

// Overloading equality operator for comparison

bool operator==(const item& i1) {

return code == i1.code;

// Overloading less than operator for sorting by code

bool operator<(const item& i1) {

return code < i1.code;

};

// Vector to store items

vector<item> o1;
void insert() {

item i1;

cout << "\nEnter item name: ";

cin >> i1.name;

cout << "Enter item quantity: ";

cin >> i1.quantity;

cout << "Enter item cost: ";

cin >> i1.cost;

cout << "Enter item code: ";

cin >> i1.code;

o1.push_back(i1);

void print(item& i1) {

cout << "\nItem Name: " << i1.name;

cout << "\nItem Quantity: " << i1.quantity;

cout << "\nItem Cost: " << i1.cost;

cout << "\nItem Code: " << i1.code;

void display() {

if (o1.empty()) {

cout << "\nNo items to display!";

} else {

for_each(o1.begin(), o1.end(), print);

void search() {

vector<item>::iterator p;

item i1;
cout << "\nEnter item code to search: ";

cin >> i1.code;

p = find(o1.begin(), o1.end(), i1);

if (p == o1.end()) {

cout << "\nItem not found!";

} else {

cout << "\nItem found:";

print(*p);

void dlt() {

vector<item>::iterator p;

item i1;

cout << "\nEnter item code to delete: ";

cin >> i1.code;

p = find(o1.begin(), o1.end(), i1);

if (p == o1.end()) {

cout << "\nItem not found!";

} else {

o1.erase(p);

cout << "\nItem deleted!";

// Sort items by code (default sort)

void sortItemsByCode() {

sort(o1.begin(), o1.end());

cout << "\nItems sorted by Code!";

}
// Sort items by cost

bool compareByCost(const item& i1, const item& i2) {

return i1.cost < i2.cost;

void sortItemsByCost() {

sort(o1.begin(), o1.end(), compareByCost);

cout << "\nItems sorted by Cost!";

int main() {

int ch;

do {

cout << "\n\n*** Menu ***";

cout << "\n1. Insert Item";

cout << "\n2. Display Items";

cout << "\n3. Search Item";

cout << "\n4. Sort Items by Cost";

cout << "\n5. Sort Items by Code";

cout << "\n6. Delete Item";

cout << "\n7. Exit";

cout << "\nEnter your choice: ";

cin >> ch;

switch (ch) {

case 1: insert(); break;

case 2: display(); break;

case 3: search(); break;

case 4: sortItemsByCost(); display(); break;

case 5: sortItemsByCode(); display(); break;

case 6: dlt(); break;


case 7: exit(0);

default: cout << "\nInvalid choice!";

} while (ch != 7);

return 0;

Output :

*** Menu ***

1. Insert Item

2. Display Items

3. Search Item

4. Sort Items by Cost

5. Sort Items by Code

6. Delete Item

7. Exit

Enter your choice: 1

Enter item name: Book

Enter item quantity: 20

Enter item cost: 100

Enter item code: 101

*** Menu ***

1. Insert Item

2. Display Items

3. Search Item

4. Sort Items by Cost

5. Sort Items by Code

6. Delete Item
7. Exit

Enter your choice: 1

Enter item name: Pen

Enter item quantity: 50

Enter item cost: 20

Enter item code: 102

*** Menu ***

1. Insert Item

2. Display Items

3. Search Item

4. Sort Items by Cost

5. Sort Items by Code

6. Delete Item

7. Exit

Enter your choice: 4

Items sorted by Cost!

Item Name: Pen

Item Quantity: 50

Item Cost: 20

Item Code: 102

Item Name: Book

Item Quantity: 20

Item Cost: 100

Item Code: 101

You might also like