unions: multiple inputs (second input overwrites first?)
Posted on 2004-11-17
Hi, i'm doing a project for my class and can't seem to figure this out.
The first part is pretty simple. We simply had to write a program in C with a union with these conditions
unsigned int ui;
union numType u;
and input a number as a float and print out the binary in 32bit representation of it as an unsigned integer using a decimal to binary converter function. However, we had to split the binary up for the signed bit, exponent (excess 127), and the fractional part or mantisa.
Now, the second part where I have a problem. We have to get the user to input 2 short integers and display them in 32bit binary representation just like the first part. In addition, we also have to do it for 2 unsigned short ints and an array of 4 characters in 32bit. So using the union:
short int s1,s2;
unsigned short int us1,us2;
unsigned char arryc;
I understand how to do this and it's supposed to be simple right? However, when I get the user to input 2 short integers the second one overwrites the first one. I'm going about this with a basic:
printf("Please enter 2 short integer numbers, 0 to exit ");
am I doing something wrong here, why can't I input both values into the union? our teacher even said that is how you setup the union, but there must be something I'm doing wrong with the way i'm trying to input the values?