
-----------------------------------
JR
Thu Jan 20, 2005 6:08 pm

database question
-----------------------------------
k lets say i got
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies entery;

how do i search inside the structure for a specifit title, year actors ect? lets say i type like 20 records inside a file using one strcutre. then i want to search the name lord of the rings as the title in all of those records and then if it finds it it ouputs all the titles with this name and then allows the user to choose one and edit it. how to do it? The records are stored inside a file.

-----------------------------------
wtd
Thu Jan 20, 2005 6:33 pm


-----------------------------------
Is this C or C++?  If it's the latter, I suggest you use the standard library a bit more and take advantage of the ability to have member functions.  Something like:

#include 
#include 

enum genre { action, romance, comedy };

class name
{
   private:
      std::string first_name, last_name;
   public:
      name(std::string f, std::string l) : first_name(f), last_name(l) {}
      std::string first() const { return first_name; }
      std::string last() const { return last_name; }
};

class actor : public name
{
   public:
      actor(std::string f, std::string l) : name(f, l) {}
};

class movie
{
   private:
      int year;
      std::string title;
      name director;
      std::vector actors;
      genre movie_genre;
   public:
      movie() { /* constructor-y goodness */ }
      // accessors and other methods
      genre get_genre() const { return movie_genre; }
};

class movie_collection : public std::vector
{
   public:
      movie_collection() : std::vector() {}
      movie_collection movies_with_genre(genre g) const
      {
         movie_collection m;
         for (int i = 0; i < size(); i++)
            if (at(i).get_genre() == g)
               m.push_back(at(i));
         return m;
      }
};

-----------------------------------
Andy
Thu Jan 20, 2005 6:41 pm


-----------------------------------
im not sure i understand ur question exactly.. but what i think ur getting at is this


struct movies{
    int year;
    char title
not sure if that helps :?

-----------------------------------
wtd
Thu Jan 20, 2005 6:53 pm


-----------------------------------
im not sure i understand ur question exactly.. but what i think ur getting at is this


struct movies{
    int year;
    char title
not sure if that helps :?

If you're forced to use C, then I highly recommend using strncpy to avoid buffer overflows.


struct movies{
    int year;
    char title

But then, string handling is probably the number one reason C is a horrible language for a newbie.

-----------------------------------
Andy
Thu Jan 20, 2005 7:16 pm


-----------------------------------
what does strncpy do?

-----------------------------------
wtd
Thu Jan 20, 2005 7:23 pm


-----------------------------------
what does strncpy do?

It's simply limits the number of characters copied.

http://man.he.net/man3/strncpy

-----------------------------------
JR
Thu Jan 20, 2005 8:01 pm


-----------------------------------
hmm ok, i still dont know how to do a search. i dont want to complicated the program too much and its C++.

-----------------------------------
wtd
Thu Jan 20, 2005 8:17 pm


-----------------------------------
hmm ok, i still dont know how to do a search. i dont want to complicated the program too much and its C++.

http://www.msoe.edu/eecs/cese/resources/stl/string.htm

The find method does what you want.

Note that I also go to the effort of lower-casing both the title and the search string, so you get a case-insensitive search.

#include 
#include 
#include 

std::string foo = "Crouching Tiger, Hidden Dragon";
std::string foo_lower_case = std::transform(foo.begin(), foo.end(), foo_lower_case.begin(), std::tolower);

std::string search_string = "Tiger";
std::string search_string_lower_case = std::transform(search_string.begin(), search_string.end(), search_string_lower_case.begin(), std::tolower);

size_type location_of_search_string = foo_lower_case.find(search_string_lower_case);

if (location_of_search_string == std::string::npos)
{
   std::cout 