Into an array of structure arrays.

Discussion in 'C++' started by Steve Chow, Feb 15, 2006.

  1. Steve Chow

    Steve Chow Guest

    rough example I have two structs

    typedef struct
    {
    int yaxis;
    int xaxis;
    int length;
    }Cords;

    typedef struct
    {
    int number;
    char type;

    Cords *cords;
    }Province;



    I have a file that looks like

    1 //Should be read into Province.number
    20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
    respectively
    21 39 80
    .......
    9 // 9 Terminates


    2
    50 80 20
    .......
    9


    So basically, I need a way of pouring an unknown amount of blocks that
    terminate with 9 from a file, into an unknown amount of Province
    structures, when it gets to the sole #9 it should move onto another
    Province structure.. ideas? (or better ideas to indicate that it should
    move onto another structure?)

    examples please.
    Steve Chow, Feb 15, 2006
    #1
    1. Advertising

  2. Steve Chow

    Artie Gold Guest

    Steve Chow wrote:
    > rough example I have two structs
    >
    > typedef struct
    > {
    > int yaxis;
    > int xaxis;
    > int length;
    > }Cords;


    Why would you use a typedef like this in C++. Unlike C, where `struct's
    have their own namespace, in C++ using the simple:

    struct Cords {
    int ...
    ...
    };

    creates a type called `Cords'.

    >
    > typedef struct
    > {
    > int number;
    > char type;
    >
    > Cords *cords;


    Why are you not using a std::vector here? It's just the Right Way (tm)
    to go.

    > }Province;
    >
    >
    >
    > I have a file that looks like
    >
    > 1 //Should be read into Province.number
    > 20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
    > respectively
    > 21 39 80
    > ......
    > 9 // 9 Terminates
    >
    >
    > 2
    > 50 80 20
    > ......
    > 9
    >
    >
    > So basically, I need a way of pouring an unknown amount of blocks that
    > terminate with 9 from a file, into an unknown amount of Province
    > structures, when it gets to the sole #9 it should move onto another
    > Province structure.. ideas? (or better ideas to indicate that it should
    > move onto another structure?)
    >
    > examples please.
    >


    Well, if you insist upon trying to do it the `C' way, you'll have to
    allocate space on the fly and use realloc() when you need more. A common
    strategy is to double the number of potential entries whenever you run
    out and do a final trim when you're done.

    Using std::vector, however, is clearly the way to go.

    ==========

    One more quick note: You should always show what you've tried; just
    asking: "How do I do it?" is not nearly as likely to get you help, nor
    is the help you get likely to be as valuable in the long run.


    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Feb 15, 2006
    #2
    1. Advertising

  3. Steve Chow

    Ben Pope Guest

    Steve Chow wrote:
    > rough example I have two structs
    >
    > typedef struct
    > {
    > int yaxis;
    > int xaxis;
    > int length;
    > }Cords;
    >
    > typedef struct
    > {
    > int number;
    > char type;
    >
    > Cords *cords;
    > }Province;


    Prefer to create the types like this:

    struct Cords {
    int yaxis;
    int xaxis;
    int length
    };

    struct Province {
    int number;
    char type;
    Cords *cords;
    };

    Thats how you do it in C++, rather than C.

    > I have a file that looks like
    >
    > 1 //Should be read into Province.number
    > 20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
    > respectively
    > 21 39 80
    > ......
    > 9 // 9 Terminates
    >
    >
    > 2
    > 50 80 20
    > ......
    > 9
    >
    >
    > So basically, I need a way of pouring an unknown amount of blocks that
    > terminate with 9 from a file, into an unknown amount of Province
    > structures, when it gets to the sole #9 it should move onto another
    > Province structure.. ideas? (or better ideas to indicate that it should
    > move onto another structure?)


    It would have been easier if you'd actually given us a sample of just
    the file, without the annotations.

    You can create a stream operators:

    #include <istream>

    istream& operator>>(istream &is, Provice &obj);
    istream& operator>>(istream &is, Coords &obj);

    Start with Coords, come back here with your code when you have that
    working :)

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Feb 15, 2006
    #3
  4. Steve Chow

    Steve Chow Guest

    "Why are you not using a std::vector here? It's just the Right Way (tm)
    to go. "

    I was aware that I could create a vector of structures, didn't think it
    was possible to have a vector of structures as a member of the
    structure. The last time I tried doing that I got some weird errors,
    I'll try again when I have access to a compiler.

    As for the rest of the stuff, I'm a C programmer who usually just
    resorts to using a few C++ things out of laziness, my knowledge of it
    as such is rather limited.

    I think I have an idea though, and will post my cade later.
    Steve Chow, Feb 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    739
    Martin Ambuhl
    Oct 26, 2004
  2. Philipp
    Replies:
    21
    Views:
    1,111
    Philipp
    Jan 20, 2009
  3. John Reynolds
    Replies:
    0
    Views:
    156
    John Reynolds
    Mar 20, 2008
  4. Cec Tre
    Replies:
    3
    Views:
    186
    Robert Klemme
    Mar 19, 2010
  5. Replies:
    2
    Views:
    257
    Rui Maciel
    Dec 12, 2012
Loading...

Share This Page