戏曲张元干送别词mp3:一个C语言问题!

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 19:55:32
#include<stdio.h>
#include<stdlib.h>
#define N 2
struct info
{
char name[20];
int age;
char job;
union
{
int class1;
char position[20];
}dept;
};
main()
{
struct info person[N];
int i;
char numstr[20];

for(i=0;i<N;i++)
{
printf("please enter data of person[%d]:\n",i);
gets(person[i].name);
gets(numstr);
person[i].age=atoi(numstr);
person[i].job=getchar();
getchar();
if(person[i].job=='s')
{
printf("input number of class1:\n");
gets(numstr);
person[i].dept.class1=atoi(numstr);
}
else if(person[i].job=='t')
{
printf("input position:\n");
gets(person[i].dept.position);
}
else printf("input error!");
}
printf("\n");
printf("name age job class/position \n");
for(i=0;i<N;i++)
if(person[i].job=='s')
printf("%-10s%d%6c%10d\n",person[i].name,person[i].age,person[i].job,
person[i].dept.class1);
else
printf("%-10s%d%6c%10s\n",person[i].name,person[i].age,person[i].job,
person[i].dept.position);
getch();
}
第27行为什么要加getchar();而不加就不能输出 person[i].dept.class1和person[i].dept.position。请帮帮忙!谢谢了!

person[i].job=getchar(); 表示输入一个字符给person[i].job,
然后对这个字符判断,
if(person[i].job=='s') //如果是这个字符是's'
person[i].dept.class1=atoi(numstr); //给person[i].dept.class1赋值,
else if(person[i].job=='t') //如果字符是't'
gets(person[i].dept.position); //输入字符串person[i].dept.position
.....
没有getchar()就没有person[i].job,没有这个字符,以上的操作都不会运行,当然 person[i].dept.class1和person[i].dept.position没有值就不能输出