Thread: A database sort of management system

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    A database sort of management system

    I'm just sort of playing around making a sort of database system, though I haven't gotten too far with it.

    I'm using a struct to hold each user's information: Name, ID, and what not.

    To keep track of things, I figured I'd need a vector to hold each struct. But then I ran into the problem of needing to name each struct uniquely and I wasn't sure how to do so. The system would allow me to add entries, so I'd need to have it dynamically create some sort of name for each struct.

    I was thinking of some sort of string to const string conversion, but I'm not not sure how to do it. Any help would be appreciated.
    Last edited by relyt_123; 06-30-2008 at 05:09 PM. Reason: Added a few points

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you mean you want to create different structs (with different member types) and name the structs like this:
    Code:
    struct data_one
    {
      string Name;
      long ID;
      string Data;
    };
    
    struct data_two
    {
      string Name;
      long ID;
      double Speed;
    };
    
    struct data_three
    {
      string Name;
      long ID;
      bool IsHappy;
      bool IsTall;
      bool IsSalty;
    };
    Or do you mean that you want to give a different name to each instance variable, like this:
    Code:
    struct data_one
    {
      string Name;
      long ID;
    };
    
    int main()
    {
      data_one firstEntry;
      data_one secondEntry;
      data_one Third_Entry;
    }
    Or do you mean that you want to be able to specify a different Name property for each instance:
    Code:
    struct data_one
    {
      string Name;
      long ID;
    };
    
    int main()
    {
      std::vector<data_one> entries(3);
      entries.at(0).Name = "Lou";
      entries.at(1).Name = "Dave";
      entries.at(2).Name = "Charlie";
    }
    If it's the first, then you probably want another solution, since creating different classes or structs on the fly isn't a good idea. If it's the second, then you don't have to give variable names if the objects are in the vector, you can just access them by their index (see the third example for an example). If it's the third, then you shouldn't have any problem.

  3. #3

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    If you're trying to make each database entry unique, then you could give each struct some unique ID.

  4. #4
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Daved View Post
    If it's the second, then you don't have to give variable names if the objects are in the vector, you can just access them by their index (see the third example for an example).
    Aha. That's exactly what I was looking for. I figured there was something similar to that to deal with it. Thanks for the help.

    In the code:
    Code:
    vector<data_one> entries(3);
    Does the (3) simply create 3 empty data_one structs at the beginning of the vector?

    How would you push back a new struct without specifying a name for it?
    Last edited by relyt_123; 06-30-2008 at 09:10 PM.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Like this:
    Code:
    entries.push_back(data_one());
    Woop?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you do
    Code:
    vector<data_one> entries(3);
    then you can access the first three items by index.

    Code:
    vector<data_one> entries(3);
    for (unsigned i = 0; i != entries.size(); ++i) {
         data_one data;
         //... fill data fields
         entries[i] = data;
    }
    But if you are going to push_back items, you may just start with an empty vector:
    Code:
    vector<data_one> entries;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  2. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  3. Why Can't C++ Be Used to Develop Operating System?
    By Antigloss in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2005, 06:16 AM
  4. How To SORT DATA In Database With C/C++?
    By javacvb in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 10:53 AM
  5. how to design a CMS (content management system) in C++ ?
    By maulikpatel in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2003, 08:37 PM