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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <string>
//#include <algorithm>
using namespace std;
string* find(string* first, string* last, string& val)
{
for (; first != last; ++first)
if (*first == val)
return first;
return last;
}
constexpr size_t ARRAY_SZ {5};
void display(const string arr[ARRAY_SZ])
{
cout << "Product # \tName\n";
for (size_t i = 0; i < ARRAY_SZ; ++i) {
cout << "[" << i << "]" << " =";
if (arr[i].empty())
cout << "\t\t-x-\n";
else
cout << "\t\t" << arr[i] << "\n";
}
}
int main()
{
string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};
display(arr);
while (true) {
string name;
//sort(arr, arr + ARRAY_SZ);
//display(arr);
cout << "Enter item name to search (CR to terminate): ";
getline(cin, name);
if (name.empty())
break;
if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
cout << "Error! Item does not exist\n";
else {
char oper {};
cout << "What do you want to do?\n[E]dit [D]elete: ";
cin >> oper;
cin.ignore(1000, '\n');
switch (oper = (char)tolower(oper)) {
case 'e':{
string newnam;
cout << "Enter new name: ";
getline(cin, newnam);
if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ){
cout << "Error! Item already exists\n";
cout << "Enter new name: ";
getline(cin, newnam);
display(arr);
}else {
*fnd = newnam;
display(arr);
}
}
break;
case 'd':
fnd->clear();
display(arr);
break;
default:
cout << "Invalid operation\n";
break;
}
}
}
}
|