unions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muser

    unions

    sorry to post so soon after having posted before. I'm using a union
    and I've read that a union can only hold a value of one member at a
    time. The following function uses a union, but i don't know whether
    the errors I'm getting is because of that, or because i have declared
    the union instances wrong, can you please help.


    struct crecord {
    char customercode[5];
    char customername[21];
    char customeraddress[61];
    char customerbalance ;
    char creditlimit;
    int Totalbalance;
    int Totalcreditlimi t;

    };

    struct irrecord {
    char customercode[5];
    char partnum[6];
    char issue_rec[5];

    };


    struct drecord {
    char customercode[5];
    };

    int loop = 200;


    union Allrecords{
    struct crecord Newcrecord;
    struct irrecord Newirrecord;
    struct drecord Newdrecord;

    };
    union Allrecords unionarray;



    void determinestruct ( union Allrecords unionarray, fstream& validdata,
    char* temp2 )
    {


    union Allrecords *str_ptr1, *str_ptr2;

    str_ptr2 = str_ptr1 + 1;

    if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
    'C')
    {
    str_ptr1 = str_ptr1.Newcre cord.customerco de, '\0';
    }

    if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
    || validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
    )
    {
    str_ptr1 = str_ptr1.Newirr ecord.customerc ode, '\0';
    }
    if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
    'D')
    {
    str_ptr1 = str_ptr1.Newdre cord.customerco de, '\0';
    }

    if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
    'C')
    {
    str_ptr2 = str_ptr2.Newcre cord.customerco de, '\0';
    }
    if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
    || validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
    )
    {
    str_ptr2 = str_ptr2.Newirr ecord.customerc ode, '\0';
    }
    if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
    'D')
    {
    str_ptr2 = str_ptr2.Newdre cord.customerco de, '\0';
    }

    }
  • Phlip

    #2
    Re: unions

    muser wrote:
    [color=blue]
    > I'm using a union
    > and I've read that a union can only hold a value of one member at a
    > time.[/color]

    There is no reason whatsoever for a programmer not working on hardware
    interface code to use unions. Stop using them, and your problem will go
    away.
    [color=blue]
    > The following function uses a union, but i don't know whether
    > the errors I'm getting is because of that, or because i have declared
    > the union instances wrong, can you please help.[/color]

    After you remove the union, report the error message you get to the mailing
    list.
    [color=blue]
    > struct crecord {
    > char customercode[5];[/color]

    Read /Accelerated C++/, and use std::string instead of character arrays for
    strings.
    [color=blue]
    > char customername[21];
    > char customeraddress[61];
    > char customerbalance ;
    > char creditlimit;
    > int Totalbalance;
    > int Totalcreditlimi t;
    >
    > };
    >
    > struct irrecord {
    > char customercode[5];
    > char partnum[6];
    > char issue_rec[5];
    >
    > };
    >
    >
    > struct drecord {
    > char customercode[5];
    > };
    >
    > int loop = 200;[/color]

    Don't put a variable up here, away from where it's used.
    [color=blue]
    > union Allrecords{
    > struct crecord Newcrecord;
    > struct irrecord Newirrecord;
    > struct drecord Newdrecord;
    >
    > };
    > union Allrecords unionarray;[/color]

    In C++, structs classes and unions occupy the same namespace as function
    names and such. You don't need to repeat 'union' here.
    [color=blue]
    > void determinestruct ( union Allrecords unionarray, fstream& validdata,
    > char* temp2 )[/color]

    That 'fstream' should be an 'istream'. Only the calling code cares if the
    stream is a file.
    [color=blue]
    > {
    >
    >
    > union Allrecords *str_ptr1, *str_ptr2;
    >
    > str_ptr2 = str_ptr1 + 1;
    >
    > if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
    > 'C')[/color]

    Use toupper() so you only need to check for 'C'.
    [color=blue]
    > {
    > str_ptr1 = str_ptr1.Newcre cord.customerco de, '\0';[/color]

    That line does not do what you think it does.
    [color=blue]
    > }
    >
    > if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
    > || validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
    > )[/color]

    While reading a binary file from a hard drive could be construed as
    "interfacin g hardware", you should instead read the code itself as a byte,
    and then create the kind of record you need from a switch statement. Then
    pass the istream into a method in each record that then fills in its values.

    Consider writing a text file with delimiters, such as \t between fields and
    \n between records. Then the record type appears in the first column, and
    each record knows how to read the subsequent columns.

    There is almost no reason to write a binary file format just to store data.

    --
    Phlip



    Comment

    • Gianni Mariani

      #3
      Re: unions

      muser wrote:[color=blue]
      > sorry to post so soon after having posted before. I'm using a union
      > and I've read that a union can only hold a value of one member at a
      > time. The following function uses a union, but i don't know whether
      > the errors I'm getting is because of that, or because i have declared
      > the union instances wrong, can you please help.
      >[/color]

      This code has ALOT of problems, many not related to the use of union.
      [color=blue]
      >
      > struct crecord {
      > char customercode[5];
      > char customername[21];
      > char customeraddress[61];
      > char customerbalance ;
      > char creditlimit;
      > int Totalbalance;
      > int Totalcreditlimi t;
      >
      > };
      >
      > struct irrecord {
      > char customercode[5];
      > char partnum[6];
      > char issue_rec[5];
      >
      > };
      >
      >
      > struct drecord {
      > char customercode[5];
      > };
      >
      > int loop = 200;
      >
      >
      > union Allrecords{
      > struct crecord Newcrecord;[/color]

      the "struct" keyword here is not needed.
      [color=blue]
      > struct irrecord Newirrecord;[/color]

      or here[color=blue]
      > struct drecord Newdrecord;[/color]

      and here.
      [color=blue]
      >
      > };
      > union Allrecords unionarray;[/color]

      the union keyword is not needed and "unionarray " does not seem to be used.
      [color=blue]
      >
      >
      >
      > void determinestruct ( union Allrecords unionarray, fstream& validdata,
      > char* temp2 )
      > {
      >
      >
      > union Allrecords *str_ptr1, *str_ptr2;[/color]

      again - no union keyword needed.
      [color=blue]
      >
      > str_ptr2 = str_ptr1 + 1;[/color]

      I don't know what you think the statement above is supposed to do but it
      makes no sense. str_ptr1 is uninitialized so if you add 1 to it, you're
      still uninitialized.

      below you seem to call "peek" many times, why ? A switch statement
      might be better.
      [color=blue]
      >
      > if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
      > 'C')
      > {
      > str_ptr1 = str_ptr1.Newcre cord.customerco de, '\0';[/color]

      I have absolutly no idea what you're trying to do here. This is using
      the "," operator and you're trying to reference a pointer as a
      struct/union. (you probably mean 'str_ptr1->').
      [color=blue]
      > }
      >
      > if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
      > || validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
      > )[/color]

      This if condition is repeated below, why ?
      [color=blue]
      > {
      > str_ptr1 = str_ptr1.Newirr ecord.customerc ode, '\0';
      > }
      > if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
      > 'D')[/color]

      This if condition is also repeated below ?
      [color=blue]
      > {
      > str_ptr1 = str_ptr1.Newdre cord.customerco de, '\0';
      > }
      >
      > if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
      > 'C')[/color]

      This if condition is repeated above ?
      [color=blue]
      > {
      > str_ptr2 = str_ptr2.Newcre cord.customerco de, '\0';
      > }
      > if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
      > || validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
      > )
      > {
      > str_ptr2 = str_ptr2.Newirr ecord.customerc ode, '\0';
      > }
      > if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
      > 'D')
      > {
      > str_ptr2 = str_ptr2.Newdre cord.customerco de, '\0';
      > }
      >
      > }[/color]

      OK - I have no idea what you think this is supposed to do but I know
      it's not going to do anything useful.

      Try simplifying the code and repost it.

      Comment

      Working...