Morning all,
My code works, but I'm having trouble implementing some logic, let me give you the run down.
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
|
void Search(std::vector<Item>& db, std::vector<std::string> args)
{
std::cout << "\n";
DispHeader();
for(unsigned int i = 0; i < db.size(); i++)
{
for(unsigned int j = 0; j < args.size(); j++)
{
if(
db[i].StockID.find(args[j]) != std::string::npos ||
db[i].Type.find(args[j]) != std::string::npos ||
db[i].Area.find(args[j]) != std::string::npos ||
db[i].Serial.find(args[j]) != std::string::npos ||
db[i].Date.find(args[j]) != std::string::npos ||
db[i].Make.find(args[j]) != std::string::npos ||
db[i].Model.find(args[j]) != std::string::npos ||
db[i].ProcessorMake.find(args[j]) != std::string::npos ||
db[i].ProcessorSpeed.find(args[j]) != std::string::npos ||
db[i].MemoryMake.find(args[j]) != std::string::npos ||
db[i].MemoryAmount.find(args[j]) != std::string::npos ||
db[i].Quantity.find(args[j]) != std::string::npos ||
db[i].Colour.find(args[j]) != std::string::npos ||
db[i].Status.find(args[j]) != std::string::npos
)
{
std::cout << db[i];
break;
}
}
}
std::cout << "\n";
}
|
Very easy function: When Search is called, the Item database (std::vector<Item>&) and Search arguments (std::vector<string>&) are passed to it. The loop iterate through all the item elements and arguments and find if they exist, if they do, they get printed out.
Here's the deal.
I want to redesign this, so each argument after the first is an && rather than an or, but I'm not sure how to implement this in a loop which has to go over multiple items and multiple arguments.
Here's an example:
> Search Laptop DELL ^D
Stock Type Area Serial In-Date Make Model CPU Speed Memory Amount Qty Colour Status
------------------------------------------------------------------------------------------------------------------------------------------------------
0001 Laptop CB-2/SH-01 01AFDCFG 01/01/2014 HP COMPAQ/2400T INTEL 2.4 KINGSTON 2.0 01 BLACK PENDING
0002 Laptop CB-2/SH-02 011FGSGD 02/01/2014 DELL INSPIRON/990 AMD 2.0 ALIENWARE 4.0 05 WHITE DONE
|
As you can see, both items are printed out, because both contain Laptop. I understand why this happens, it's because I use || on each loop. I want to search function to see the arguments as Laptop AND DELL.
I feel like I need some ORAND logic or something, or I'm just missing something.
Hope this has made sense.
Any help or nudge is appreciated.
EDIT:
After staring at this blankly for a few minutes, How about a little recursion?
1 2 3 4 5 6 7
|
...
std::vector<Item> Temp;
if(...) Temp.push_back(...); // Each Item which contains an arg is added to this
...
//When done, Search calls itself with the new list of items, and checks for the next argument on this list
args.pop_back();
Search(Temp,args);
|
UPDATE:
I think I am on the right path here, however it seg faults right after it displays the results. Also my first attempt at recursion.
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
|
void Search(std::vector<Item>& db, std::vector<std::string>& args)
{
std::cout << "\n";
static std::vector<Item> temp;
for(unsigned int i = args.size()-1; i > 0;)
{
for(unsigned int j = 0; j < db.size(); j++)
{
if(
db[j].StockID.find(args[i]) != std::string::npos ||
db[j].Type.find(args[i]) != std::string::npos ||
db[j].Area.find(args[i]) != std::string::npos ||
db[j].Serial.find(args[i]) != std::string::npos ||
db[j].Date.find(args[i]) != std::string::npos ||
db[j].Make.find(args[i]) != std::string::npos ||
db[j].Model.find(args[i]) != std::string::npos ||
db[j].ProcessorMake.find(args[i]) != std::string::npos ||
db[j].ProcessorSpeed.find(args[i]) != std::string::npos ||
db[j].MemoryMake.find(args[i]) != std::string::npos ||
db[j].MemoryAmount.find(args[i]) != std::string::npos ||
db[j].Quantity.find(args[i]) != std::string::npos ||
db[j].Colour.find(args[i]) != std::string::npos ||
db[j].Status.find(args[i]) != std::string::npos
)
{
temp.push_back(db[i]);
//std::cout << db[i];
break;
}
}
args.pop_back();
if(args.size() > 0) Search(temp,args);
else
{
DispHeader();
for(unsigned int found = 0; found < temp.size(); found++) std::cout << temp[found];
std::cout << "\n";
}
}
}
|
UPDATE 2:
This is my new solution, seems to work as is, feedback appreciated.
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
|
void Search(std::vector<Item>& db, std::vector<std::string>& args)
{
std::cout << "\n";
std::vector<Item> temp;
for(unsigned int j = 0; j < db.size(); j++)
{
if(
db[j].StockID.find(args[args.size()-1]) != std::string::npos ||
db[j].Type.find(args[args.size()-1]) != std::string::npos ||
db[j].Area.find(args[args.size()-1]) != std::string::npos ||
db[j].Serial.find(args[args.size()-1]) != std::string::npos ||
db[j].Date.find(args[args.size()-1]) != std::string::npos ||
db[j].Make.find(args[args.size()-1]) != std::string::npos ||
db[j].Model.find(args[args.size()-1]) != std::string::npos ||
db[j].ProcessorMake.find(args[args.size()-1]) != std::string::npos ||
db[j].ProcessorSpeed.find(args[args.size()-1]) != std::string::npos ||
db[j].MemoryMake.find(args[args.size()-1]) != std::string::npos ||
db[j].MemoryAmount.find(args[args.size()-1]) != std::string::npos ||
db[j].Quantity.find(args[args.size()-1]) != std::string::npos ||
db[j].Colour.find(args[args.size()-1]) != std::string::npos ||
db[j].Status.find(args[args.size()-1]) != std::string::npos
)
{
temp.push_back(db[j]);
}
}
args.pop_back();
if(args.size() > 0) Search(temp,args);
else
{
DispHeader();
for(unsigned int found = 0; found < temp.size(); found++) std::cout << temp[found];
std::cout << "\n";
return;
}
}
|