|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小泉向西流 于 2014-6-6 14:36 编辑
今天看了小甲鱼老师的c语言的视频,第059讲, 讲的共用体部分的内容。
- #include<stdio.h>
- struct
- {
- int num;
- char name[20];
- char sex;
- char job;
- union
- {
- int class;
- char position[10];
- }category;
- }person[2];
- void main()
- {
- int i, n;
- printf("Please input the number of the records: ");
- scanf("%d",&n);
- printf("Please input records!\n\n");
- for(i=0;i<n;i++)
- {
- printf("Please input the num: ");
- scanf("%d",&person[i].num);
- printf("Please input the name: ");
- scanf("%s",person[i].name);
- printf("Please input the sex <M/F>: ");
- scanf("%c",&person[i].sex);
- printf("Please input the job <t/s>: ");
- scanf("%c",&person[i].job);
- if(person[i].job=='s')
- {
- printf("Plaese input the class: ");
- scanf("%d",&person[i].category.class);
- }
- else if(person[i].job=='t')
- {
- printf("Plaese input the position: ");
- scanf("%s",person[i].category.position);
- }
- else
- {
- printf("inpur error!\n\n");
- }
-
-
- }
-
-
-
- printf("\n\n");
- printf("There are %d records:\n", n);
- printf("No.\tname\tsex\tjob\tclass/position\n");
- for(i=0;i<n;i++)
- {
- if(person[i].job=='s')
- {
- printf("%-6d %-8s %-7c %-6c %-6d\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.class);
- }
- else
- {
- printf("%-6d %-8s %-7c %-6c %-6s\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.position);
- }
- }
- }
复制代码
输出结果:
提示输出不正常,导致输入也不正常。
但编译可以通过,无error 无warning。
为什么会出现这种情况呢?求大神帮忙开导下。
|
|