1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <cstring>
#include <iterator>
constexpr size_t MAXCHAR {200};
constexpr size_t CAPACITY {20};
bool removeItem(char itemNames[][MAXCHAR], float itemPrice[], size_t& size, const char delItem[]) {
size_t i {};
bool remove {};
for (i = 0; i < size; ++i)
if (std::strcmp(itemNames[i], delItem) == 0) {
remove = true;
break;
}
if (!remove) {
std::cout << delItem << " is not currently in inventory\n";
return false;
}
--size;
for (size_t j = i; j < size; ++j) {
strcpy(itemNames[j], itemNames[j + 1]);
itemPrice[j] = itemPrice[j + 1];
}
std::cout << delItem << " has been removed from inventory.\n";
return true;
}
int main() {
char items[][MAXCHAR] {"it", "was", "the", "best", "of", "times"};
float price[] {1, 2, 3, 4, 5, 6};
auto sz {std::size(items)};
char delItem[CAPACITY] {};
static_assert(std::size(items) == std::size(price));
std::cout << "Please enter the name of the item to be deleted from inventory: ";
std::cin.getline(delItem, CAPACITY);
removeItem(items, price, sz, delItem);
for (size_t i = 0; i < sz; ++i)
std::cout << items[i] << " " << price[i] << '\n';
std::cout << '\n';
}
|