I suspect the assignment is to create a sorted list and search it. Hence, don’t recommend using standard algorithms... assume OP is smart enough to use them if he/she were permitted and/or instructed to do so.
@
excavator
You are confusing yourself by mixing tasks. I recommend you create a simple menu loop like this:
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
|
const int MAX_ITEM_COUNT = 100;
cont int NUM_FIELDS = 4; // name, email, IC, password
std::string list[MAX_ITEM_COUNT][NUM_FIELDS];
int count = 0; // number of items in list[]
int main()
{
bool done = false;
while (!done)
{
std::cout << "\n"
"Menu:\n"
"1. Add Item\n"
"2. Find Item\n"
"3. List Items\n"
"0. Quit\n"
"\n"
"Choice? ";
int choice;
std::cin >> choice;
switch (choice)
{
case 1: add_item(); break;
case 2: find_item(); break;
case 3: list_items(); break;
case 0: done = true;
default:
}
}
}
| |
You need to create each of the three functions. Each function should do its one and only thing, then be done.
The things that you can do are up to you, of course. This is just an example.
Remember that the
count
(number of items in the list, 0 to 100) is not the same as the number of fields. When searching,
i
starts at zero and ends at
count
:
|
for (int i = 0; i < count; i++)
| |
but the
j
iterates through the
fields of each item: it starts at zero and ends at NUM_FIELDS (or whatever you call it):
1 2 3 4 5 6 7
|
for (int i = 0; i < count; i++)
{
for (int j = 0; j < NUM_FIELDS; j++)
{
...
}
}
| |
Unless you are specifically tasked to sort the items because the
user asked for it (by name, perhaps?), you really do not need to sort. Find where the item is to be inserted in the list, then shift every item over one and insert the new item in the right spot. This is a little trickier with a 2D array, but it works using the very same logic as inserting an integer in an array of integers — something you should have been required to do before now.
Hope this helps.