#include <stdio.h>
#include <string.h>
struct item {
char name[50];
int quantity;
};
const int MAX_ITEMS = 10;
struct item warehouse[MAX_ITEMS];
int item_count = 0;
void addItem() {
struct item newItem;
printf("Enter item name: ");
fgets(newItem.name, sizeof(newItem.name), stdin);
newItem.name[strlen(newItem.name) - 1] = '\0'; // Remove newline
printf("Enter item quantity: ");
scanf("%d", &newItem.quantity);
if (item_count < MAX_ITEMS) {
strcpy(warehouse[item_count].name, newItem.name);
warehouse[item_count].quantity = newItem.quantity;
item_count++;
printf("Item added to warehouse.\n");
} else {
printf("Warehouse is full. Cannot add more items.\n");
}
}
void retrieveItem() {
char itemName[50];
printf("Enter item name to retrieve: ");
fgets(itemName, sizeof(itemName), stdin);
itemName[strlen(itemName) - 1] = '\0'; // Remove newline
int i;
for (i = 0; i < item_count; i++) {
if (strcmp(warehouse[i].name, itemName) == 0) {
printf("Found %s. Available quantity: %d\n", itemName, warehouse[i].quantity);
return;
}
}
printf("Item not found in warehouse.\n");
}
int main() {
int choice;
while (1) {
printf("\nWarehouse Management System\n");
printf("1. Add Item\n");
printf("2. Retrieve Item\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addItem();
break;
case 2:
retrieveItem();
break;
case 3:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please select again.\n");
}
}
return 0;
}
Please note that this is a very simplified example. In real-world applications, you'd need more advanced features, error handling, and probably a database or some form of persistent storage. Also, remember that using C for such tasks can be more complex and less safe than using higher-level languages with built-in features for managing data structures and memory.