Need help adding a search function.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Need help adding a search function.

 
0
  #1
Jul 22nd, 2008
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!

  1. #include <iostream>
  2. #include <string> // Include this...
  3. #include <vector>
  4. #include<fstream>
  5. using namespace std;
  6.  
  7. // Constants
  8. static const string ADD = string("add");
  9. static const string LIST = string("list");
  10. static const string EXIT = string("exit");
  11. static const string SEARCH = string("search");//My search declarer thing
  12.  
  13. // No need for global strings here
  14.  
  15.  
  16. class objects {
  17. public:
  18. objects() {} // This can be left out, it will be automatically generated
  19. ~objects() {} // Same
  20. std::string name;
  21. }; // Don't need to create some global object here
  22.  
  23. int main(int argc, char* argv[])
  24. {
  25. vector<objects*> vect;
  26.  
  27. // We'll use this to refer to all objects we create
  28. objects *object_pointer;
  29.  
  30. // This is generally how we'll push objects...
  31. object_pointer = new objects;
  32. object_pointer->name = "Hello World!";
  33. vect.push_back(object_pointer);
  34.  
  35. cout <<"type add to add an object.\n";
  36. cout <<"type list to see the current list of objects.\n";
  37. cout <<"type exit to leave.\n";
  38.  
  39. string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt
  40.  
  41. while (input != EXIT)
  42. {
  43. cout << endl << ">> ";
  44.  
  45. getline(cin, input);
  46.  
  47. if (input == ADD)
  48. {
  49. fstream file_op("c:\\List.txt",ios::out);
  50. cout <<"specify object's name: ";
  51.  
  52. string test;
  53. getline(cin, test);
  54.  
  55. object_pointer = new objects;
  56. object_pointer->name = test;
  57. vect.push_back(object_pointer);
  58.  
  59.  
  60. file_op<<"Test Write to file";
  61. file_op.close();
  62. }
  63. else if (input == LIST)
  64. {
  65. char str[2000];
  66. fstream file_op("c:\\List.txt",ios::in);
  67. while(file_op >> str)
  68. cout << str ;
  69.  
  70.  
  71. cout << endl << "objects ";
  72.  
  73. // Pretty typical way of iterating here
  74. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  75. cout << endl << (*iter)->name;
  76.  
  77. file_op.close();//close
  78.  
  79. }
  80. +#include <iostream>
  81. #include <string> // Include this...
  82. #include <vector>
  83. #include<fstream>
  84. using namespace std;
  85.  
  86. // Constants
  87. static const string ADD = string("add");
  88. static const string LIST = string("list");
  89. static const string EXIT = string("exit");
  90. static const string SEARCH = string("search");
  91.  
  92. // No need for global strings here
  93.  
  94.  
  95. class objects {
  96. public:
  97. objects() {} // This can be left out, it will be automatically generated
  98. ~objects() {} // Same
  99. std::string name;
  100. }; // Don't need to create some global object here
  101.  
  102. int main(int argc, char* argv[])
  103. {
  104. vector<objects*> vect;
  105.  
  106. // We'll use this to refer to all objects we create
  107. objects *object_pointer;
  108.  
  109. // This is generally how we'll push objects...
  110. object_pointer = new objects;
  111. object_pointer->name = "Hello World!";
  112. vect.push_back(object_pointer);
  113.  
  114. cout <<"type add to add an object.\n";
  115. cout <<"type list to see the current list of objects.\n";
  116. cout <<"type exit to leave.\n";
  117.  
  118. string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt
  119.  
  120. while (input != EXIT)
  121. {
  122. cout << endl << ">> ";
  123.  
  124. getline(cin, input);
  125.  
  126. if (input == ADD)
  127. {
  128. fstream file_op("c:\\List.txt",ios::out);
  129. cout <<"specify object's name: ";
  130.  
  131. string test;
  132. getline(cin, test);
  133.  
  134. object_pointer = new objects;
  135. object_pointer->name = test;
  136. vect.push_back(object_pointer);
  137.  
  138.  
  139. file_op<<"Test Write to file";
  140. file_op.close();
  141. }
  142. else if (input == LIST)
  143. {
  144. char str[2000];
  145. fstream file_op("c:\\List.txt",ios::in);
  146. while(file_op >> str)
  147. cout << str ;
  148.  
  149.  
  150. cout << endl << "objects ";
  151.  
  152. // Pretty typical way of iterating here
  153. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  154. cout << endl << (*iter)->name;
  155.  
  156. file_op.close();//close
  157.  
  158. }
  159. //----------------------------------------------------------------------
  160. else if (input == SEARCH)//Here is the problem!
  161. {
  162. cout << "What object do you want?";
  163. //I want to add a search function but I am not clear on how to do it.
  164. }
  165. }
  166.  
  167. //-----------------------------------------------------------------------------------
  168. // Free up our allocated memory
  169. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  170. delete (*iter)++;
  171.  
  172. if (!vect.empty())
  173. vect.clear();
  174.  
  175. return 0;
  176. }
  177. else if (input == SEARCH)
  178. {
  179. cout << "What object do you want?";
  180. for(vector<objects
  181. }
  182. }
  183.  
  184. // Free up our allocated memory
  185. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  186. delete (*iter)++;
  187.  
  188. if (!vect.empty())
  189. vect.clear();
  190.  
  191. return 0;
  192. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Need help adding a search function.

 
1
  #2
Jul 22nd, 2008
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 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?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Need help adding a search function.

 
0
  #3
Jul 23rd, 2008
I'm not quite clear on how to do that...
This is what I got so far.
  1. else if (input == SEARCH)
  2. {
  3. cout << "What object do you want?";
  4. std::vector - scan::iterator iter=vect.begin();iter != vect.end();iter++)
  5.  
  6.  
  7. if (iter=vector.end())}
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Need help adding a search function.

 
0
  #4
Jul 23rd, 2008
What am I doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help adding a search function.

 
1
  #5
Jul 23rd, 2008
Originally Posted by Pikachumanson View Post
I'm not quite clear on how to do that...
This is what I got so far.
  1. else if (input == SEARCH)
  2. {
  3. cout << "What object do you want?";
  4. std::vector - scan::iterator iter=vect.begin();iter != vect.end();iter++)
  5.  
  6.  
  7. 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.

  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. int SearchVector(vector <string> theStrings, string aString);
  8.  
  9. int main ()
  10. {
  11. vector <string> theStrings;
  12. string aString = "Hi";
  13. theStrings.push_back(aString);
  14. aString = "Howdy";
  15. theStrings.push_back(aString);
  16. aString = "Hello";
  17. theStrings.push_back(aString);
  18.  
  19. string lookForThis = "Howdy";
  20. cout << SearchVector(theStrings, lookForThis) << endl;
  21. lookForThis = "Hello World!";
  22. cout << SearchVector(theStrings, lookForThis) << endl;
  23.  
  24. return 0;
  25. }
  26.  
  27.  
  28. int SearchVector(vector <string> theStrings, string aString)
  29. // returns index if found, -1 if not found
  30. {
  31. for (int i = 0; i < theStrings.size(); i++)
  32. {
  33. string thisString = theStrings[i];
  34. if (thisString.compare(aString) == 0)
  35. return i; // found at index i
  36. }
  37.  
  38. return -1; // not found
  39. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Need help adding a search function.

 
0
  #6
Jul 24th, 2008
Thanks ArkM and Vernon, your posts have helped me alot!
But I have one more point of contention.

Here it is.
  1. else if (input == SEARCH)
  2. {
  3. cout << "What object do you want?";
  4. string s1 = "T";
  5. string s2 = "S";
  6. string s3 = "TestOne";
  7. cout << "Object s1 == s2 " << (s1 == s2) << endl;
  8. cout << "Object s2 == s3 " << (s2 == s3) << endl;
  9. cout << "Object s1 == s3 " << (s1 == s3) << endl;
  10. cout << "The object you found is " << endl;
  11. cout << endl << (*iter)->name == objects;//Problem is here!
  12. operator==(name);//Do I need this here?
  13. return 1;
  14.  
  15.  
  16. }
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?
Last edited by Pikachumanson; Jul 24th, 2008 at 12:46 am. Reason: Left out the problem with my code
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 242
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Need help adding a search function.

 
0
  #7
Jul 24th, 2008
Shouldn't that be either (*iter).name or iter->name
ssharish
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Need help adding a search function.

 
1
  #8
Jul 24th, 2008
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:
  1. int SearchVector(const vector<objects*>& allObjects, const string& aString);
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Need help adding a search function.

 
0
  #9
Jul 24th, 2008
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.
  1. #include <iostream>
  2. #include <string> // Include this...
  3. #include <algorithm>
  4. #include <vector>
  5. #include<fstream>
  6. using namespace std;
  7.  
  8. // Constants
  9. static const string ADD = string("add");
  10. static const string LIST = string("list");
  11. static const string EXIT = string("exit");
  12. static const string SEARCH = string("search");
  13.  
  14. // No need for global strings here
  15.  
  16.  
  17. class objects {
  18. public:
  19. objects() {} // This can be left out, it will be automatically generated
  20. ~objects() {} // Same
  21. std::string name;
  22. std::string find;
  23. }; // Don't need to create some global object here
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27. int SearchVector(const vector<objects*>& allObjects, const string& aString);//gotta figure out the parameters for this, don't think i need it though
  28. vector<objects*> vect;
  29. vector<int>::iterator iter;
  30.  
  31.  
  32.  
  33.  
  34.  
  35. // We'll use this to refer to all objects we create
  36. objects *object_pointer;
  37.  
  38. // This is generally how we'll push objects...
  39. object_pointer = new objects;
  40. object_pointer->name = "Hello World!";
  41. vect.push_back(object_pointer);
  42. object_pointer->find = " ";//new iterator
  43. cout <<"type add to add an object.\n";
  44. cout <<"type list to see the current list of objects.\n";
  45. cout <<"type exit to leave.\n";
  46. cout <<"type search to search for objects";
  47. string input(""); // I'm not sure if C++ auto-initializes strings, but this doesn't hurt
  48.  
  49. while (input != EXIT)
  50. {
  51. cout << endl << ">> ";
  52.  
  53. getline(cin, input);
  54.  
  55. if (input == ADD)
  56. {
  57. cout <<"specify object's name: ";
  58.  
  59. string test;
  60. getline(cin, test);
  61.  
  62. object_pointer = new objects;
  63. object_pointer->name = test;
  64. vect.push_back(object_pointer);
  65.  
  66.  
  67. }
  68. else if (input == LIST)
  69. {
  70.  
  71.  
  72.  
  73. cout << endl << "objects ";
  74.  
  75. // Pretty typical way of iterating here
  76. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  77. cout << endl << (*iter)->name;
  78.  
  79.  
  80. }
  81. else if (input == SEARCH)
  82. {
  83. string find;
  84. object_pointer->find;
  85. cout << "What object do you want?";
  86. getline(cin, find);//What this does is return whatever I put in. That's no good for me
  87. cout << "The object you found is " << find<< endl;
  88.  
  89.  
  90.  
  91.  
  92.  
  93. return 1;
  94.  
  95.  
  96. }
  97. }
  98.  
  99. // Free up our allocated memory
  100. for(vector<objects*>::iterator iter=vect.begin();iter != vect.end();iter++)
  101. delete (*iter)++;
  102.  
  103. if (!vect.empty())
  104. vect.clear();
  105.  
  106. return 0;
  107. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Need help adding a search function.

 
0
  #10
Jul 24th, 2008
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:
  1. int SearchObjects(const std::vector<objects*>& pall, const std::string what)
  2. {
  3. int n = 0;
  4. for (int i = 0; i < pall.size(); ++i)
  5. if (what == pall[i]->name)
  6. ++n;
  7. return n;
  8. }
  9. ...
  10. // search command handling
  11. ...
  12. int n = SearchObjects(vect,find);
  13. if (n)
  14. cout << n << " object" << (n>1?"s":"") << " here" << std::endl;
  15. else
  16. cout << "*** Object not found" << endl;
  17. ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC