By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

unions

P: n/a
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 Totalcreditlimit;

};

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.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr1 = str_ptr1.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr1 = str_ptr1.Newdrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr2 = str_ptr2.Newcrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdrecord.customercode, '\0';
}

}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
muser wrote:
I'm using a union
and I've read that a union can only hold a value of one member at a
time.
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.
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.
After you remove the union, report the error message you get to the mailing
list.
struct crecord {
char customercode[5];
Read /Accelerated C++/, and use std::string instead of character arrays for
strings.
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};

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

};
struct drecord {
char customercode[5];
};

int loop = 200;
Don't put a variable up here, away from where it's used.
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords unionarray;
In C++, structs classes and unions occupy the same namespace as function
names and such. You don't need to repeat 'union' here.
void determinestruct( union Allrecords unionarray, fstream& validdata,
char* temp2 )
That 'fstream' should be an 'istream'. Only the calling code cares if the
stream is a file.
{
union Allrecords *str_ptr1, *str_ptr2;

str_ptr2 = str_ptr1 + 1;

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
Use toupper() so you only need to check for 'C'.
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
That line does not do what you think it does.
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)


While reading a binary file from a hard drive could be construed as
"interfacing 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
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
Jul 22 '05 #2

P: n/a
muser wrote:
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.

This code has ALOT of problems, many not related to the use of union.

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

};

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;
the "struct" keyword here is not needed.
struct irrecord Newirrecord;
or here struct drecord Newdrecord;
and here.

};
union Allrecords unionarray;
the union keyword is not needed and "unionarray" does not seem to be used.

void determinestruct( union Allrecords unionarray, fstream& validdata,
char* temp2 )
{
union Allrecords *str_ptr1, *str_ptr2;
again - no union keyword needed.

str_ptr2 = str_ptr1 + 1;
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.

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
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->').
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
This if condition is repeated below, why ?
{
str_ptr1 = str_ptr1.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
This if condition is also repeated below ?
{
str_ptr1 = str_ptr1.Newdrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
This if condition is repeated above ?
{
str_ptr2 = str_ptr2.Newcrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdrecord.customercode, '\0';
}

}


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.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.