an array OF structs??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atsukoarai86
    New Member
    • Mar 2008
    • 6

    an array OF structs??

    I'm having a severe problem here. I need to read data from a sequential file and my programmer friend tells me i need to use an "array of structs" but he is being incredibly vague about this particular syntax. so, let's say I have this:

    #include<iostre am>
    #include<fstrea m>
    #include<cstdli b>

    using namespace std;

    struct myData
    {
    int employeeID;
    char lastName[15]
    char firstName[10];
    float payRate;
    };

    ______________

    let's say I have a known 10 records in this file that I need to read into the members of this struct. How do I declare this "array of structs", if this is indeed possible?

    Thanks
    ~Atusko
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    First off, you're missing a semicolon there, in your first array declaration. Second, an array of structs is declared like any other array. However, none of the contained data members are initialized, as you can't pass arguments to any constructors this way.

    [CODE=cpp]
    struct foo{
    int a;
    char* b;
    }

    int main(){
    foo bar[10]; //Allocates an array of 10 foo objects
    return 0;
    }
    [/CODE]

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I'm more interested in the format of the file you are trying to read. An array of structs is fine but how yuo populate that array is dependent upon the format of the input file.

      If you do not know that fornat, then the data you read can't be put in the correct struct member.

      Comment

      Working...