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