unions

M

muser

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';
}

}
 
P

Phlip

muser said:
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.
 
G

Gianni Mariani

muser said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

built in types 4
Short program 3
std::sort whole program 2
function error 6
Access violation error 10
Reading a file 1
Reading a file. 3
changing char value 4

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top