• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Stuck on a c++ problem, need HELP!

xchangx

Golden Member
I'm stuck on a problem here. Kinda long...

We're inputting from a file, one line at a time, into a character array (char input[191]).

in line there are 5 key fields and then 3 fields that repeat 10 times. I have everything setup in a struct with a few unions.

I need to setup an index for each field and would like to put it in the struct. See below...

However, I can't seem to figure out how to put the offset variable into the struct without screwing up the the initial array.

Here's the code for the struct:

struct
{
int byte_offset[10];
union
{
char input[191];
union
{
struct
{
char trans_code[2];
char emp_id[10];
char date[7];
char clk_in[6];
char clk_out[6];
struct
{
char qty[4];
char invent_no[7];
char dev_no[5];
}repeat[10];
}time_card;
struct
{
char del_trans_code[2];
char key_data[45];
char repeating[144];
}output;
};
}rec;
}record;

Please help!

Thanks

Michael
 
You have too many nested structs and unions that I can't even understand your code. I would create a single struct for each record/line that you are inputing from the file. After you do that, then you can rearange your data however it makes sense to you. Looking at your code, it's not clear exactly what you are trying to do.
 
Originally posted by: xchangx
I'm stuck on a problem here. Kinda long...

We're inputting from a file, one line at a time, into a character array (char input[191]).

in line there are 5 key fields and then 3 fields that repeat 10 times. I have everything setup in a struct with a few unions.

I need to setup an index for each field and would like to put it in the struct. See below...

However, I can't seem to figure out how to put the offset variable into the struct without screwing up the the initial array.

Here's the code for the struct:

struct
{
int byte_offset[10];
union
{
char input[191];
union
{
struct
{
char trans_code[2];
char emp_id[10];
char date[7];
char clk_in[6];
char clk_out[6];
struct
{
char qty[4];
char invent_no[7];
char dev_no[5];
}repeat[10];
}time_card;
struct
{
char del_trans_code[2];
char key_data[45];
char repeating[144];
}output;
};
}rec;
}record;

Please help!

Thanks

Michael


Define structures outside the main area and then reference them internally. This is easier to read and understand. You have three primary structures. I defined them outside the acutal memory allocation area.
Now just use each structure definition as if it was an actual variable. Easier to read. Then afterwords it may be clearer to you and others to understand your problem


// Output Structure
typedef struct output_structure
{
char del_trans_code[2];
char key_data[45];
char repeating[144];
};

// Repeat Structure
typedef struct repeated_structure
{
char qty[4];
char invent_no[7];
char dev_no[5];
};

// Timecard Structure
typedef struct timecard_structure
{
char trans_code[2];
char emp_id[10];
char date[7];
char clk_in[6];
char clk_out[6];

repeated_structure repeat[10];
};

// Record definition Structure
typedef struct record_structure
{
union
{
timecard_structure time_card;
output_structure output;
};
} rec;

// Processing Record
struct
{
int byte_offset[10];
union
{
char input[191];
record_structure rec;
};
}record;


 
Back
Top