search and save

Hey guys I need help with this assignment. So far I have the add function and the list function done correctly but I dont know how to do the search function and save it into a new file. PLEASE HELP! Thank you in advance. This is the assignment:

A database system capable of storing information about
a moderately large collection of things, allowing
insertion of new items, and answering simple search
queries.

You choose what the things stored in the database are,
but don't make them too similar to the people we used
in the class examples.

The individual things should be implemented as C++
objects (structs or classes), and the entire database
whould be an array of pointers to those objects.

When your program starts, it should read the database
from a text file. It is up to you whether you create your
own data file or use an existing one that you find somewhere.

When running, your program should offer the user a choice
of actions to perform. The choices must include adding
a new item to the database, listing all items that match
a particular input, and stopping.

Adding a new item:
The user will have to type all the data fields by hand.
Your program has to create a new object, and add it at the
end of the database array if there is enough space.

Search:
The user will enter a string, and your program will
find all the database items that match.
But this is where you have to do a little bit of work.
Your program should have TWO arrays of pointers to data
objects. One is for the whole database itself, the other
is to temporarily store (pointers to) the items that match
the search term.
Your search function should be given as parameters
the whole database array and the extra results array.
It should put into the results array pointers to all
the matching objects.
The matching objects should be printed only after the
search function has finished its job.

Exit:
If any new items were added, the database should be
saved back into a text file.
It might be a good idea to create a new file instead of
overwriting the original. If your program goes wrong you
will be quite annoyed if you have to create your data
collection again.

and this is my code:

# include <iostream>
# include <string>
# include <fstream>
# include "library.h"
using namespace std;

const int size=1000;

struct allstars
{
int year;
string first, last;
};
allstars *read(istream &m)
{
string fi, la;
int yr;
m>>yr>>fi>>la;
if (m.fail())
return NULL;
allstars*a=new allstars;
(*a).year=yr;
(*a).first=fi;
(*a).last=la;
return a;
}

allstars *database[size];
int num;

void read()
{
ifstream data("data.txt");
if (data.fail())
{
cout<<"Wrong file.\n";
return;
}
num=0;
while(true)
{
database[num]=read(data);
if(database[num]==NULL) break;
num+=1;
}
data.close();
}

void add()
{
cout<<"Enter the Year, then the first name, then the last name: ";
database[num]=read(cin);
num+=1;
}

void list(ostream &m)
{
for(int i=0; i<num; i+=1)
{
m<<(*database[i]).year<<" "<<(*database[i]).first<<" "<<(*database[i]).last<<endl;
}
}

void search()
{
cout<<"Enter Year or first name then last name: ";
}

void leave()
{
cout<<"exiting now\n";
}
void choices()
{
read();
while(true)
{ cout<<"Choose from the following, enter your choice as 1, 2, 3, or 4:\n"
<<"1) if you want to add\n"
<<"2) if you want to search\n"
<<"3) if you want to list\n"
<<"4) if you want to exit\n\n";
int x;
cin>> x;
if (x==1)
{
add();
}
else if (x==2)
{
search();
}
else if (x==3)
{
list(cout);
}
else if (x==4)
{
leave();
break;
}
else
{
cout<<"Typed an incorrect key.\n";
cin>> x;
}
}
}

void main()
{
choices();
}
Topic archived. No new replies allowed.