Hello everyone, as you can see this is my first post here.Here is what I want to do, When I type add the program adds a new object in the form of word or whatever else you type type to the list function. I want to make a search function that when you type the word search you are then prompted to type in an object from the list. Then the word pops up on the screen. Does anyone have any ideas or examples? Note I don't want to search for anything that is hardcoded in the program I already know how to do that. I would like to search for objects that I define through the add function.Thank you for your time.
Regards, Pikachumanson
Here is my code:
#include <iostream>
#include <string> // Include this...
#include <algorithm>
#include <vector>
#include<fstream>
using namespace std;
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)//My problem is here!
{
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;
You need to go through all object names and compare it with the user given search word. Here is the modified code. Comments are included in the modified portion
#include <iostream>
#include <string> // Include this...
#include <algorithm>
#include <vector>
#include<fstream>
usingnamespace std;
// Constants
staticconst string ADD = string("add");
staticconst string LIST = string("list");
staticconst string EXIT = string("exit");
staticconst 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);
}
elseif (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;
}
elseif (input == SEARCH)//My problem is here!
{
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;
//@@@ Modifile Here
//We need to go through all stored objects and compare object names with the user given word
bool found=false;
for(vector<objects*>::iterator i=vect.begin();i != vect.end();i++){
if((*i)->name==find){
cout<< "Yes We found the object: " << find <<"\n";
found=true;
break;
}
}//
if(found){
break; // break the while loop and back to main, clean up and return
}else{
cout << "We could not find the word: " << find <<"\n";
}
//@@ Modified End
}
}
// 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;
}