#include <stdio.h>
#include <conio.h>
struct schllo
{
int num;
char name[20];
char sex;
char job;
union
{
int banji;
char position[10];
}category;
}person[2];
void main()
{
int i;
for(i = 0; i<2; i++)
{
printf("Please input the number: ");
scanf("%d", &person[i].num);
printf("Please input the name: ");
scanf("%s", &person[i].name);
printf("Please input the sex(W/M): ");
scanf("%c", &person[i].sex);
printf("Please input the job: ");
scanf("%c", &person[i].job);
if(person[i].job == 't')
{
printf("Please input the position: ");
scanf("%s", &person[i].category.position);
}
else if(person[i].job == 's')
{
printf("Please input the class: ");
scanf("%d", &person[i].category.banji);
}
else
{
printf("input errpr\n");
}
}
for(i=0; i<2; i++)
{
if(person[i].job == 's')
{
printf("num: %d\nname: %s\nsex: %c\njob: %c\nclass: %d", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.banji);
}
else if(person[i].job == 't')
{
printf("num: %d\nname: %s\nsex: %c\njob: %c\nposition: %s", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.position);
}
}
}
程序如上,关于联合体和结构体的一些小测试,编译没有任何问题,但是在执行的时候,输入姓名name之后sex(性别)出现了问题,没有输入直接跳转到了下一个选项(即job),于是我凌乱了,求解答,不胜感激~~
#include <conio.h>
struct schllo
{
int num;
char name[20];
char sex;
char job;
union
{
int banji;
char position[10];
}category;
}person[2];
void main()
{
int i;
for(i = 0; i<2; i++)
{
printf("Please input the number: ");
scanf("%d", &person[i].num);
printf("Please input the name: ");
scanf("%s", &person[i].name);
printf("Please input the sex(W/M): ");
scanf("%c", &person[i].sex);
printf("Please input the job: ");
scanf("%c", &person[i].job);
if(person[i].job == 't')
{
printf("Please input the position: ");
scanf("%s", &person[i].category.position);
}
else if(person[i].job == 's')
{
printf("Please input the class: ");
scanf("%d", &person[i].category.banji);
}
else
{
printf("input errpr\n");
}
}
for(i=0; i<2; i++)
{
if(person[i].job == 's')
{
printf("num: %d\nname: %s\nsex: %c\njob: %c\nclass: %d", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.banji);
}
else if(person[i].job == 't')
{
printf("num: %d\nname: %s\nsex: %c\njob: %c\nposition: %s", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.position);
}
}
}
程序如上,关于联合体和结构体的一些小测试,编译没有任何问题,但是在执行的时候,输入姓名name之后sex(性别)出现了问题,没有输入直接跳转到了下一个选项(即job),于是我凌乱了,求解答,不胜感激~~