My assignment was to write a program to ask for the user's name and if they were a U.S. citizen. If they are, ask them for the state they are from and input the data in a union. If they are not, ask what country they are from and input the data into the same union.
I have written my code, but the If-Else statement in my DataEnter() function doesn't seem to be working properly. It keeps evaluating to "true" no matter what I type in. Any help would be appreciated. Below is the code I have written:
Code:
#include <stdio.h>
struct bit_field {
int us_cit: 1;
int non_us_cit: 1;
};
struct personal {
char name[20];
struct bit_field citizen;
union {
char state[20];
char country[20];
} nationality;
} info;
typedef struct personal PER;
void DataEnter(PER *ptr);
void DataDisplay(PER *ptr);
int main(void)
{
DataEnter(&info);
DataDisplay(&info);
return 0;
}
// DataEnter() definition
void DataEnter(PER *ptr)
{
char is_yes[4];
printf("Please enter your name:\n");
gets(ptr->name);
printf("Are you a U.S. citizen: (Yes or No)\n");
gets(is_yes);
if ((is_yes[0] == 'Y') || (is_yes[0] = 'y')) {
printf("Please enter the name of the state you are from:\n");
gets(ptr->nationality.state);
ptr->citizen.us_cit = 1;
ptr->citizen.non_us_cit = 0;
}
else {
printf("Please enter the name of the country you are from:\n");
gets(ptr->nationality.country);
ptr->citizen.non_us_cit = 1;
ptr->citizen.us_cit = 0;
}
}
// DataDisplay() definition
void DataDisplay(PER *ptr)
{
printf("\nBelow is a list of the information you have entered:\n");
printf("Name: %s\n", ptr->name);
if (ptr->citizen.us_cit && !ptr->citizen.non_us_cit)
printf("State: %s\n", ptr->nationality.state);
else if (!ptr->citizen.us_cit && ptr->citizen.non_us_cit)
printf("Country: %s\n", ptr->nationality.country);
}