0% found this document useful (0 votes)
4 views4 pages

Second Round C++ Array Function

The document contains two C++ programs: a Library Management System and an Inventory Management System. The Library Management System allows users to add, display, issue, and return books, while the Inventory Management System enables users to add items, display them, update quantities, and calculate total inventory value. Both systems utilize arrays to manage data and provide a menu-driven interface for user interaction.

Uploaded by

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

Second Round C++ Array Function

The document contains two C++ programs: a Library Management System and an Inventory Management System. The Library Management System allows users to add, display, issue, and return books, while the Inventory Management System enables users to add items, display them, update quantities, and calculate total inventory value. Both systems utilize arrays to manage data and provide a menu-driven interface for user interaction.

Uploaded by

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

1.

Library Management System


#include <iostream>
#include <string>
using namespace std;

const int MAX_BOOKS = 100;


string titles[MAX_BOOKS];
int ids[MAX_BOOKS];
bool issued[MAX_BOOKS];
int bookCount = 0;

void addBook() {
if (bookCount < MAX_BOOKS) {
cout << "Enter book title: ";
cin.ignore(); ‫ الداله دي بتعمل تجاهل حرف السطر الجديد المتبقي عشان بيبقي في‬buffer ‫قبل استخدام‬getline
getline(cin, titles[bookCount]); ‫طبعا بنستخدم الداله دي عشان نقدر نكتب سطر كامل في مسافات‬

‫وعشان نستخدم االتنين دول الزم نستدعي مكتبه‬String


cout << "Enter book ID: ";
cin >> ids[bookCount];
issued[bookCount] = false;
bookCount++;
cout << "Book added!\n";
} else {
cout << "Library is full.\n";
}
}

void displayBooks() {
for (int i = 0; i < bookCount; i++) {
cout << i + 1 << ". " << titles[i] << " (ID: " << ids[i] << ")";
if (issued[i]) cout << " - Issued\n";
else cout << " - Available\n";
}
}

int findBookById(int id) {


for (int i = 0; i < bookCount; i++)
if (ids[i] == id) return i;
return -1;
}
void issueBook() {
int id;
cout << "Enter book ID to issue: ";
cin >> id;
int idx = findBookById(id);
if (idx != -1 && !issued[idx]) {
issued[idx] = true;
cout << "Book issued.\n";
} else {
cout << "Book not found or already issued.\n";
}
}

void returnBook() {
int id;
cout << "Enter book ID to return: ";
cin >> id;
int idx = findBookById(id);
if (idx != -1 && issued[idx]) {
issued[idx] = false;
cout << "Book returned.\n";
} else {
cout << "Book not found or not issued.\n";
}
}

int main() {
int choice;
do {
cout << "\nLibrary Menu:\n1. Add Book\n2. Display Books\n3. Issue Book\n4. Return
Book\n0. Exit\nChoice: ";
cin >> choice;
switch (choice) {
case 1: addBook(); break;
case 2: displayBooks(); break;
case 3: issueBook(); break;
case 4: returnBook(); break;
case 0: cout << "Goodbye!\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 0);
}
2. Inventory Management System
#include <iostream>
#include <string>
using namespace std;

const int MAX_ITEMS = 100;


string itemNames[MAX_ITEMS];
int quantities[MAX_ITEMS];
float prices[MAX_ITEMS];
int itemCount = 0;

void addItem() {
if (itemCount < MAX_ITEMS) {
cout << "Enter item name: ";
cin.ignore();
getline(cin, itemNames[itemCount]);
cout << "Enter quantity: ";
cin >> quantities[itemCount];
cout << "Enter price: ";
cin >> prices[itemCount];
itemCount++;
cout << "Item added!\n";
} else {
cout << "Inventory is full.\n";
}
}

void displayItems() {
for (int i = 0; i < itemCount; i++) {
cout << i + 1 << ". " << itemNames[i]
<< " | Qty: " << quantities[i]
<< " | Price: $" << prices[i] << "\n";
}
}

int findItemByName(string name) {


for (int i = 0; i < itemCount; i++)
if (itemNames[i] == name) return i;
return -1;
}

void updateQuantity() {
string name;
cout << "Enter item name to update: ";
cin.ignore();
getline(cin, name);
int idx = findItemByName(name);
if (idx != -1) {
cout << "Enter new quantity: ";
cin >> quantities[idx];
cout << "Quantity updated.\n";
} else {
cout << "Item not found.\n";
}
}

void calculateTotalValue() {
float total = 0;
for (int i = 0; i < itemCount; i++)
total += quantities[i] * prices[i];
cout << "Total inventory value: $" << total << "\n";
}

int main() {
int choice;
do {
cout << "\nInventory Menu:\n1. Add Item\n2. Display Items\n3. Update Quantity\n4.
Calculate Total Value\n0. Exit\nChoice: ";
cin >> choice;
switch (choice) {
case 1: addItem(); break;
case 2: displayItems(); break;
case 3: updateQuantity(); break;
case 4: calculateTotalValue(); break;
case 0: cout << "Goodbye!\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 0);
}

You might also like