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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
int getid(I_List &temp)
{
enum tokSwitch { search, list, add, edit };
tokSwitch cstatus, lstatus; // current status and last status
string input; // to get inputs as needed
int iindex; // item index to be passed to edit item
cstatus = search; // default to search
if (temp.I_count() < 10)
cstatus = list; // unless there are less than 20 (arbitrary number) items
while (true) // infinate loop
{
switch (cstatus) // what we do depends on cstatus
{
case search:
{
// prompt user with options and ask for search string
cout << endl << "You can enter a partial name,";
cout << " '-c' to cancel, or '-l' to list all items." << endl;
cout << "Enter search string: ";
getline (cin, input); // get input from user
if (I_tolower(input.substr(0,2)) == "-c") // if user chose cancel
return -1; // return -1 to calling function
if (I_tolower(input.substr(0,2)) == "-l") // if user chose list
{
cstatus = list; // change to list status
break; // go to next iteration of loop
}
vector<int> sindex; // to store the indexes from searching
int i = temp.I_getindex(input, 0); // get first index
while (i != -1) // while indexes are found
{
sindex.push_back(i); // add to our search results
i = temp.I_getindex(input, i+1); // and get next result
}
if (sindex.size() == 0) // if there are no results
cout << endl << "No results found." << endl;
else // results found
{
cout << endl << sindex.size() << " result(s) found." << endl;
for (int i = 0; i < sindex.size(); i++) // list results
cout << i + 1 << ". " << temp.I_getitem(sindex.at(i)).name << endl;
cout << endl << "Enter the menu number for the item you want, or" << endl;
}
// give options to user
cout << "Type '-c' to cancel, '-l' to list all items, '-a' to add an item" << endl;
if (sindex.size() > 0) // if results, give option to edit one of them
cout << "or '-e #' to edit one of the above items." << endl;
cout << endl << "Enter option: ";
getline (cin, input); // get user input
if (isdigit(input.at(0))) // if we have a number
{
int i = atoi (input.c_str()); // convert to integer
if (i-1 < sindex.size()) // if it's a valid number
return temp.I_getitem(sindex.at(i-1)).id; // return the ID
cout << endl << "Invalid Number!" << endl; // invalid number
break; // go to next iteration of loop
}
if (I_tolower(input.substr(0,2)) == "-c") // if user chose cancel
break; // at this point, just go to next iteration of loop
if (I_tolower(input.substr(0,2)) == "-l") // if user chose to list
{
cstatus = list; // change status to list
break; // and go to next iteration of loop
}
if (I_tolower(input.substr(0,2)) == "-a") // if user chose to add
{
lstatus = search; // set last status to search
cstatus = add; // set current status to add
break; // go to next iteration of loop
}
if (I_tolower(input.substr(0,2)) == "-e") // if user chose to edit
{
if ((input.size() > 3) && (isdigit(input.at(3)))) // if '-e #'
{
int i = atoi (input.substr(3).c_str()); // convert to integer
if (i-1 < sindex.size()) // if number is valid
{
iindex = sindex.at(i-1); // set item index to item to edit
lstatus = search; // set last status to search
cstatus = edit; // set current status to edit
break; // go to next iteration of loop
}
}
}
cout << endl << "Invalid Option!" << endl;
break; // just go to next iteration of loop
}
case list:
{
int i = temp.I_count(); // get size of list
cout << "There are " << i << " items in the list." << endl;
if (i > 20) // if count is higher than 20
i = (i / 2) + (i % 2); // split it in half
for (int j = 0; j < i; j++)
{
cout << j << ". " << temp.I_getitem(j).name;
if ((j+i) < temp.I_count()) // second column
{
for (int k = temp.I_getitem(j).name.size(); k < 38; k++)
cout << " "; // pad with spaces
cout << j + i << ". " << temp.I_getitem(j+i).name;
}
cout << endl; // and add endl to each line
}
// promt user and get input etc..
return -1;
}
case add:
{
cout << "Not implemented yet." << endl;
return -1;
}
case edit:
{
cout << "Not implemented yet." << endl;
return -1;
}
}
}
}
|