| | |
Need help adding a search function.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Ok here is what I got so far.
When you type add, it asks what object would you like to add
When you type list, it lists those objects
When you type exit, you leave the program
What I would like to do is create a search function where I can type in the name of an object that is on the list and it will come up on the screen.
I would like at LEAST an explanation of how this would be done if I can't have an example.
I know it has something to do with a loop and iteration but my mind is drawing a blank right now. Any help would be greatly appreciated!
When you type add, it asks what object would you like to add
When you type list, it lists those objects
When you type exit, you leave the program
What I would like to do is create a search function where I can type in the name of an object that is on the list and it will come up on the screen.
I would like at LEAST an explanation of how this would be done if I can't have an example.
I know it has something to do with a loop and iteration but my mind is drawing a blank right now. Any help would be greatly appreciated!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> // Include this... #include <vector> #include<fstream> using namespace std; // Constants static const string ADD = string("add"); static const string LIST = string("list"); static const string EXIT = string("exit"); static const string SEARCH = string("search");//My search declarer thing // No need for global strings here class objects { public: objects() {} // This can be left out, it will be automatically generated ~objects() {} // Same std::string name; }; // Don't need to create some global object here int main(int argc, char* argv[]) { vector<objects*> vect; // We'll use this to refer to all objects we create objects *object_pointer; // This is generally how we'll push objects... object_pointer = new objects; object_pointer->name = "Hello World!"; vect.push_back(object_pointer); cout <<"type add to add an object.\n"; cout <<"type list to see the current list of objects.\n"; cout <<"type exit to leave.\n"; string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt while (input != EXIT) { cout << endl << ">> "; getline(cin, input); if (input == ADD) { fstream file_op("c:\\List.txt",ios::out); cout <<"specify object's name: "; string test; getline(cin, test); object_pointer = new objects; object_pointer->name = test; vect.push_back(object_pointer); file_op<<"Test Write to file"; file_op.close(); } else if (input == LIST) { char str[2000]; fstream file_op("c:\\List.txt",ios::in); while(file_op >> str) cout << str ; cout << endl << "objects "; // Pretty typical way of iterating here for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) cout << endl << (*iter)->name; file_op.close();//close } +#include <iostream> #include <string> // Include this... #include <vector> #include<fstream> using namespace std; // Constants static const string ADD = string("add"); static const string LIST = string("list"); static const string EXIT = string("exit"); static const string SEARCH = string("search"); // No need for global strings here class objects { public: objects() {} // This can be left out, it will be automatically generated ~objects() {} // Same std::string name; }; // Don't need to create some global object here int main(int argc, char* argv[]) { vector<objects*> vect; // We'll use this to refer to all objects we create objects *object_pointer; // This is generally how we'll push objects... object_pointer = new objects; object_pointer->name = "Hello World!"; vect.push_back(object_pointer); cout <<"type add to add an object.\n"; cout <<"type list to see the current list of objects.\n"; cout <<"type exit to leave.\n"; string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt while (input != EXIT) { cout << endl << ">> "; getline(cin, input); if (input == ADD) { fstream file_op("c:\\List.txt",ios::out); cout <<"specify object's name: "; string test; getline(cin, test); object_pointer = new objects; object_pointer->name = test; vect.push_back(object_pointer); file_op<<"Test Write to file"; file_op.close(); } else if (input == LIST) { char str[2000]; fstream file_op("c:\\List.txt",ios::in); while(file_op >> str) cout << str ; cout << endl << "objects "; // Pretty typical way of iterating here for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) cout << endl << (*iter)->name; file_op.close();//close } //---------------------------------------------------------------------- else if (input == SEARCH)//Here is the problem! { cout << "What object do you want?"; //I want to add a search function but I am not clear on how to do it. } } //----------------------------------------------------------------------------------- // Free up our allocated memory for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) delete (*iter)++; if (!vect.empty()) vect.clear(); return 0; } else if (input == SEARCH) { cout << "What object do you want?"; for(vector<objects } } // Free up our allocated memory for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) delete (*iter)++; if (!vect.empty()) vect.clear(); return 0; }
The only way to search in std::vector - scan the vector with iterator (from begin() upto end())) and compare search string with (*iter)->name.
If you want fast (better than linear) search, see std::map class with find member function.
Apropos, it seems you need
- Dereference iterator - get a reference to pointer to object (from the vector) - delete the object via this pointer- increase THIS POINTER in the vector. Why?
If you want fast (better than linear) search, see std::map class with find member function.
Apropos, it seems you need
delete *iter; , not delete (*iter)++; . See what happens in your case:- Dereference iterator - get a reference to pointer to object (from the vector) - delete the object via this pointer- increase THIS POINTER in the vector. Why?
I'm not quite clear on how to do that...
This is what I got so far.
This is what I got so far.
C++ Syntax (Toggle Plain Text)
else if (input == SEARCH) { cout << "What object do you want?"; std::vector - scan::iterator iter=vect.begin();iter != vect.end();iter++) if (iter=vector.end())}
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
I'm not quite clear on how to do that...
This is what I got so far.
C++ Syntax (Toggle Plain Text)
else if (input == SEARCH) { cout << "What object do you want?"; std::vector - scan::iterator iter=vect.begin();iter != vect.end();iter++) if (iter=vector.end())}
Is this the code? Are you subtracting an iterator from a vector? I don't think ArkM intended you to literally use "vector - scan" in the code. I think that was just a hyphen in the sentence. You have a vector of strings that you want to search through. As ArkM suggested, go through the vector one element at a time and compare the element to the string you are looking for. The code below uses a regular old loop rather than an iterator, but it's the same concept. Unlike Java, where you are provided a function that searches the vector to see if an element is in it, in C++ you have to write that function yourself.
C++ Syntax (Toggle Plain Text)
#include <string> #include <vector> #include <iostream> using namespace std; int SearchVector(vector <string> theStrings, string aString); int main () { vector <string> theStrings; string aString = "Hi"; theStrings.push_back(aString); aString = "Howdy"; theStrings.push_back(aString); aString = "Hello"; theStrings.push_back(aString); string lookForThis = "Howdy"; cout << SearchVector(theStrings, lookForThis) << endl; lookForThis = "Hello World!"; cout << SearchVector(theStrings, lookForThis) << endl; return 0; } int SearchVector(vector <string> theStrings, string aString) // returns index if found, -1 if not found { for (int i = 0; i < theStrings.size(); i++) { string thisString = theStrings[i]; if (thisString.compare(aString) == 0) return i; // found at index i } return -1; // not found }
Thanks ArkM and Vernon, your posts have helped me alot!
But I have one more point of contention.
Here it is.
This is the error I get:
1>c:\users\hector rosario\documents\visual studio 2008\projects\rosario_week9\rosario_week9\dictionary.cpp(91) : error C2227: left of '->name' must point to class/struct/union/generic type
1> type is 'int'
What exactly does that mean and even better how the heck do I fix it?
But I have one more point of contention.
Here it is.
C++ Syntax (Toggle Plain Text)
else if (input == SEARCH) { cout << "What object do you want?"; string s1 = "T"; string s2 = "S"; string s3 = "TestOne"; cout << "Object s1 == s2 " << (s1 == s2) << endl; cout << "Object s2 == s3 " << (s2 == s3) << endl; cout << "Object s1 == s3 " << (s1 == s3) << endl; cout << "The object you found is " << endl; cout << endl << (*iter)->name == objects;//Problem is here! operator==(name);//Do I need this here? return 1; }
1>c:\users\hector rosario\documents\visual studio 2008\projects\rosario_week9\rosario_week9\dictionary.cpp(91) : error C2227: left of '->name' must point to class/struct/union/generic type
1> type is 'int'
What exactly does that mean and even better how the heck do I fix it?
Last edited by Pikachumanson; Jul 24th, 2008 at 12:46 am. Reason: Left out the problem with my code
1. I don't understand what do YOU want in the code after [icode]cout << "What object do you want?";[/code]. May be, come back to design stage (or earlier)? In actual fact, your objects have the only attribute - name. The user can identify (type in) object name. Possible search results: yes or no, found or not found? Think before coding...
2. It seems no properly declared iterators in this point. Please, present current version of your code (don't forget code tags).
3. Try to use Vernon's SearchVector function (adopt it for vector<objects> - apropos, strange class name for only object with a name). May be, better prototype (and header) for this function is:
2. It seems no properly declared iterators in this point. Please, present current version of your code (don't forget code tags).
3. Try to use Vernon's SearchVector function (adopt it for vector<objects> - apropos, strange class name for only object with a name). May be, better prototype (and header) for this function is:
C++ Syntax (Toggle Plain Text)
int SearchVector(const vector<objects*>& allObjects, const string& aString);
Ok here is what I want to do. "Hello world" is on my list of objects by default. When I type in "list" Hello world should come up. Now I type in "add" and it aks what object would you like to add. So I type in "Hellboy". Now when I type in list, "Hello world" and "Hellboy" should come up.
This part I have down pat.
The problem I have been wrestling with for the past two days is now I want to input "search"
and type in one of the existing items that is on the list
I want to input "Hellboy" and then have the word pop up on the screen.
Vernon's example is good for predefined objects that are hard coded into the program but I am trying to find objects that have been created due to the add function.
This part I have down pat.
The problem I have been wrestling with for the past two days is now I want to input "search"
and type in one of the existing items that is on the list
I want to input "Hellboy" and then have the word pop up on the screen.
Vernon's example is good for predefined objects that are hard coded into the program but I am trying to find objects that have been created due to the add function.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> // Include this... #include <algorithm> #include <vector> #include<fstream> using namespace std; // Constants static const string ADD = string("add"); static const string LIST = string("list"); static const string EXIT = string("exit"); static const string SEARCH = string("search"); // No need for global strings here class objects { public: objects() {} // This can be left out, it will be automatically generated ~objects() {} // Same std::string name; std::string find; }; // Don't need to create some global object here int main(int argc, char* argv[]) { int SearchVector(const vector<objects*>& allObjects, const string& aString);//gotta figure out the parameters for this, don't think i need it though vector<objects*> vect; vector<int>::iterator iter; // We'll use this to refer to all objects we create objects *object_pointer; // This is generally how we'll push objects... object_pointer = new objects; object_pointer->name = "Hello World!"; vect.push_back(object_pointer); object_pointer->find = " ";//new iterator cout <<"type add to add an object.\n"; cout <<"type list to see the current list of objects.\n"; cout <<"type exit to leave.\n"; cout <<"type search to search for objects"; string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt while (input != EXIT) { cout << endl << ">> "; getline(cin, input); if (input == ADD) { cout <<"specify object's name: "; string test; getline(cin, test); object_pointer = new objects; object_pointer->name = test; vect.push_back(object_pointer); } else if (input == LIST) { cout << endl << "objects "; // Pretty typical way of iterating here for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) cout << endl << (*iter)->name; } else if (input == SEARCH) { string find; object_pointer->find; cout << "What object do you want?"; getline(cin, find);//What this does is return whatever I put in. That's no good for me cout << "The object you found is " << find<< endl; return 1; } } // Free up our allocated memory for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++) delete (*iter)++; if (!vect.empty()) vect.clear(); return 0; }
Vernon's SearchVector() function presents one of standard (universal) search patterns. It's a pity that you see this forum only as a copy/paste source of ready to use codes.
I think, your search use case is a rather questionable one. Your object is a name only now (no other attributes). It seems you plan a strange dialogue:
System: (shows menu)...
User: search
System: specify object's name:
User: Hellboy
System: Hellboy
They have called their system Parrot++...
Moreover, it's possible to add many objects with the same name in your vector (you have no control over this aspect of the system behaviour)...
Well, let's go on:
I think, your search use case is a rather questionable one. Your object is a name only now (no other attributes). It seems you plan a strange dialogue:
System: (shows menu)...
User: search
System: specify object's name:
User: Hellboy
System: Hellboy
They have called their system Parrot++...
Moreover, it's possible to add many objects with the same name in your vector (you have no control over this aspect of the system behaviour)...
Well, let's go on:
C++ Syntax (Toggle Plain Text)
int SearchObjects(const std::vector<objects*>& pall, const std::string what) { int n = 0; for (int i = 0; i < pall.size(); ++i) if (what == pall[i]->name) ++n; return n; } ... // search command handling ... int n = SearchObjects(vect,find); if (n) cout << n << " object" << (n>1?"s":"") << " here" << std::endl; else cout << "*** Object not found" << endl; ...
![]() |
Similar Threads
- Link-listed addressbook (C)
- Deleting characters function ( with just one parameter) (C)
- Recursion II (C++)
- Recursion (C++)
- C programming - need some help (C)
- help with this code of link list (C++)
- Search Script help (PHP)
- help with php and javascript (PHP)
- new member here :) (C)
Other Threads in the C++ Forum
- Previous Thread: Need Help with a question
- Next Thread: queue ?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






