鱼C论坛

 找回密码
 立即注册
查看: 520|回复: 3

[已解决]结构体与共用体的问题

[复制链接]
发表于 2020-8-9 11:27:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 名字什么的随便 于 2020-8-9 11:33 编辑
  1. #include <stdio.h>

  2. struct system
  3. {
  4.         int number;
  5.         char name[20];
  6.         char sex;
  7.         char job;
  8.         union
  9.         {
  10.                 char classroom[20];//学生
  11.                 char position[20];//老师
  12.         }category;
  13. }person[2];

  14. void main()
  15. {
  16.         int i;
  17.         for(i=0;i<2;i++)//输入
  18.         {
  19.                 printf("请输入第%d名成员的号码:",i+1);
  20.                 scanf("%d",&person[i].number);

  21.                 printf("请输入第%d名成员的姓名:",i+1);
  22.                 scanf("%s",person[i].name);

  23.                 printf("请输入第%d名成员的性别(F/M):",i+1);//F is female,M is male
  24.                 scanf("%c",&person[i].sex);

  25.                 printf("请输入第%d名成员的职业(T/S):",i+1);//T is teacher,S is student
  26.                 scanf("%c",&person[i].job);

  27.                 if(person[i].job=='T')
  28.                 {
  29.                         printf("请输入第%d名成员的教务:",i+1);
  30.                         scanf("%s",person[i].category.position);
  31.                 }
  32.                 if(person[i].job=='S')
  33.                 {
  34.                         printf("请输入第%d名成员的班级:",i+1);
  35.                         scanf("%s",person[i].category.classroom);
  36.                 }
  37.         }

  38.         for(i=0;i<2;i++)//输入
  39.         {
  40.                 printf("Input info: ");
  41.                 scanf("%d %s %c %c", &person[i].number, person[i].name, &(person[i].sex), &(person[i].job));
  42.         if(person[i].job == 'S'){  //如果是学生
  43.             scanf("%s",person[i].category.classroom);
  44.         }else{  //如果是老师
  45.             scanf("%s",person[i].category.position);
  46.         }
  47.         }
  48. }
复制代码

如果使用第一个for循环的输入方式时为什么会出现性别无法输入的情况,而第二个for循环则没有???编译器是visualc++6.0的。
最佳答案
2020-8-9 12:10:43
因为用第二种方式程序会以为你输入的性别、职业全都属于 person[ i ].name 这个字符串。而且你第一种方式也有点问题,帮你改了改:

  1. #include <stdio.h>

  2. struct system
  3. {
  4.         int number;
  5.         char name[20];
  6.         char sex;
  7.         char job;
  8.         union
  9.         {
  10.                 char classroom[20];//学生
  11.                 char position[20];//老师
  12.         }category;
  13. }person[2];

  14. int main()
  15. {
  16.         int i;
  17.         for(i=0;i<2;i++)//输入
  18.         {
  19.                 printf("请输入第%d名成员的号码:",i+1);
  20.                 scanf("%d",&person[i].number);

  21.                 printf("请输入第%d名成员的姓名:",i+1);
  22.                 scanf("%s",person[i].name);
  23.                 getchar(); // 过滤换行
  24.                 printf("请输入第%d名成员的性别(F/M):",i+1);//F is female,M is male
  25.                 scanf("%c",&person[i].sex);
  26.                 getchar(); // 过滤换行
  27.                 printf("请输入第%d名成员的职业(T/S):",i+1);//T is teacher,S is student
  28.                 scanf("%c",&person[i].job);

  29.                 if(person[i].job=='T')
  30.                 {
  31.                         printf("请输入第%d名成员的教务:",i+1);
  32.                         scanf("%s",person[i].category.position);
  33.                 }
  34.                 if(person[i].job=='S')
  35.                 {
  36.                         printf("请输入第%d名成员的班级:",i+1);
  37.                         scanf("%s",person[i].category.classroom);
  38.                 }
  39.         }
  40. return 0;
  41. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-9 11:27:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-9 12:09:10 | 显示全部楼层
本帖最后由 巴巴鲁 于 2020-8-9 12:10 编辑

输入完姓名后按回车才能输入性别,但是你按了回车,程序会认为你按的回车就是输入的性别
在27行前加入
  1. getchar();  // 缓冲输入的回车子符
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-9 12:10:43 | 显示全部楼层    本楼为最佳答案   
因为用第二种方式程序会以为你输入的性别、职业全都属于 person[ i ].name 这个字符串。而且你第一种方式也有点问题,帮你改了改:

  1. #include <stdio.h>

  2. struct system
  3. {
  4.         int number;
  5.         char name[20];
  6.         char sex;
  7.         char job;
  8.         union
  9.         {
  10.                 char classroom[20];//学生
  11.                 char position[20];//老师
  12.         }category;
  13. }person[2];

  14. int main()
  15. {
  16.         int i;
  17.         for(i=0;i<2;i++)//输入
  18.         {
  19.                 printf("请输入第%d名成员的号码:",i+1);
  20.                 scanf("%d",&person[i].number);

  21.                 printf("请输入第%d名成员的姓名:",i+1);
  22.                 scanf("%s",person[i].name);
  23.                 getchar(); // 过滤换行
  24.                 printf("请输入第%d名成员的性别(F/M):",i+1);//F is female,M is male
  25.                 scanf("%c",&person[i].sex);
  26.                 getchar(); // 过滤换行
  27.                 printf("请输入第%d名成员的职业(T/S):",i+1);//T is teacher,S is student
  28.                 scanf("%c",&person[i].job);

  29.                 if(person[i].job=='T')
  30.                 {
  31.                         printf("请输入第%d名成员的教务:",i+1);
  32.                         scanf("%s",person[i].category.position);
  33.                 }
  34.                 if(person[i].job=='S')
  35.                 {
  36.                         printf("请输入第%d名成员的班级:",i+1);
  37.                         scanf("%s",person[i].category.classroom);
  38.                 }
  39.         }
  40. return 0;
  41. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 18:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表