Author |
Message |
JR
|
Posted: Thu Jan 20, 2005 6:08 pm Post subject: 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.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Thu Jan 20, 2005 6:33 pm Post subject: (No subject) |
|
|
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:
code: | #include <string>
#include <vector>
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<actor> 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<movie>
{
public:
movie_collection() : std::vector<movie>() {}
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
|
Posted: Thu Jan 20, 2005 6:41 pm Post subject: (No subject) |
|
|
im not sure i understand ur question exactly.. but what i think ur getting at is this
c++: |
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies movie1;
strcpy("Lord of The Rings",movie1.title[10]);
|
not sure if that helps
|
|
|
|
|
 |
wtd
|
Posted: Thu Jan 20, 2005 6:53 pm Post subject: (No subject) |
|
|
Andy wrote: im not sure i understand ur question exactly.. but what i think ur getting at is this
c++: |
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies movie1;
strcpy("Lord of The Rings",movie1.title[10]);
|
not sure if that helps 
If you're forced to use C, then I highly recommend using strncpy to avoid buffer overflows.
c++: |
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies movie1;
strncpy("Lord of The Rings", movie1.title[10], 256);
|
But then, string handling is probably the number one reason C is a horrible language for a newbie.
|
|
|
|
|
 |
Andy
|
Posted: Thu Jan 20, 2005 7:16 pm Post subject: (No subject) |
|
|
what does strncpy do?
|
|
|
|
|
 |
wtd
|
Posted: Thu Jan 20, 2005 7:23 pm Post subject: (No subject) |
|
|
Andy wrote: what does strncpy do?
It's simply limits the number of characters copied.
http://man.he.net/man3/strncpy
|
|
|
|
|
 |
JR
|
Posted: Thu Jan 20, 2005 8:01 pm Post subject: (No subject) |
|
|
hmm ok, i still dont know how to do a search. i dont want to complicated the program too much and its C++.
|
|
|
|
|
 |
wtd
|
Posted: Thu Jan 20, 2005 8:17 pm Post subject: (No subject) |
|
|
JR wrote: 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.
code: | #include <algorithm>
#include <string>
#include <iostream> |
code: | 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 << "Not found." << std::endl;
}
else
{
std::cout << "Found! Search string begins at: " << location_of_search_string << std::endl;
} |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Andy
|
Posted: Thu Jan 20, 2005 8:40 pm Post subject: (No subject) |
|
|
what complier are you using? maybe i can post some of my databases so u can take a look
|
|
|
|
|
 |
wtd
|
Posted: Thu Jan 20, 2005 8:46 pm Post subject: (No subject) |
|
|
Andy wrote: what complier are you using? maybe i can post some of my databases so u can take a look
I'm using G++, but that code will work with any standards-compliant C++ compiler.
|
|
|
|
|
 |
Andy
|
Posted: Thu Jan 20, 2005 8:46 pm Post subject: (No subject) |
|
|
o not you wtd lol.. u dont need to see my crappy programs, i was talking to JR
|
|
|
|
|
 |
JR
|
Posted: Thu Jan 20, 2005 10:40 pm Post subject: (No subject) |
|
|
devC++
|
|
|
|
|
 |
Andy
|
Posted: Fri Jan 21, 2005 7:54 am Post subject: (No subject) |
|
|
Ooo.. i dont think my code will work then.. but you can take a look just the same... here i'll post the exe version too, see insdie of going through the list one by one, i used a binary tree to sort my contacts.. i dont know how much u noe about them, but they're quite useful
dl
Description: |
|
 Download |
Filename: |
Love Bytes exe.rar |
Filesize: |
104.74 KB |
Downloaded: |
217 Time(s) |
|
|
|
|
|
 |
wtd
|
Posted: Fri Jan 21, 2005 4:24 pm Post subject: (No subject) |
|
|
Note: if you're trying to demonstrate how you solved a problem, the executable version tells people very very little.
|
|
|
|
|
 |
Andy
|
Posted: Sat Jan 22, 2005 3:12 pm Post subject: (No subject) |
|
|
thats y the source is attatched too under the dl link
|
|
|
|
|
 |
|