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!

#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 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.

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())}

I'm not quite clear on how to do that...
This is what I got so far.

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.

#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.

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;
  
		
		}

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?

Shouldn't that be either (*iter).name or iter->name ssharish

  1. I don't understand what do YOU want in the code after cout << "What object do you want?";. 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:

int SearchVector(const vector<objects*>& allObjects, const string& aString);

commented: Yes. const modifiers should have been in my function. +5

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.

#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:

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;
...

I'm sorry if I made it seem I was here to copy and paste... THat surely wasn't the case. If it was like that what am I doing trying to become a programmer in the first place? If I don't understand something, I don't understand something. I know I got more than more than a little annoying with my problem and I apologize. I'm still learning, as we all are.
Anyway, I thank you all helping me out. The solution worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.