鱼C论坛

 找回密码
 立即注册
查看: 2623|回复: 2

c语言--共用体函数--操作输入输出时不正常的问题。

[复制链接]
发表于 2014-6-5 20:57:11 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 小泉向西流 于 2014-6-6 14:36 编辑

今天看了小甲鱼老师的c语言的视频,第059讲, 讲的共用体部分的内容。
QQ截图20140605205157.png


  1. #include<stdio.h>


  2. struct
  3. {
  4.         int num;
  5.         char name[20];
  6.         char sex;
  7.         char job;
  8.         union
  9.         {
  10.                 int class;
  11.                 char position[10];
  12.         }category;
  13. }person[2];

  14. void main()
  15. {
  16.         int  i, n;
  17.         printf("Please input the number of the records:  ");
  18.         scanf("%d",&n);

  19.         printf("Please input records!\n\n");
  20.         for(i=0;i<n;i++)
  21.         {
  22.                 printf("Please input the num: ");
  23.                 scanf("%d",&person[i].num);               
  24.                 printf("Please input the name: ");
  25.                 scanf("%s",person[i].name);
  26.             printf("Please input the sex <M/F>: ");
  27.                 scanf("%c",&person[i].sex);        
  28.                 printf("Please input the job <t/s>: ");
  29.                 scanf("%c",&person[i].job);

  30.                 if(person[i].job=='s')
  31.                 {
  32.                         printf("Plaese input the class: ");
  33.                         scanf("%d",&person[i].category.class);
  34.                 }
  35.                 else if(person[i].job=='t')
  36.                 {
  37.                         printf("Plaese input the position: ");
  38.                         scanf("%s",person[i].category.position);
  39.                 }
  40.                 else
  41.                 {
  42.                         printf("inpur error!\n\n");
  43.                 }
  44.                
  45.                
  46.         }
  47.         
  48.         
  49.         
  50.         printf("\n\n");
  51.         printf("There are %d records:\n", n);
  52.         printf("No.\tname\tsex\tjob\tclass/position\n");
  53.         for(i=0;i<n;i++)
  54.         {
  55.                 if(person[i].job=='s')
  56.                 {
  57.                         printf("%-6d  %-8s %-7c %-6c %-6d\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.class);
  58.                 }
  59.                 else
  60.                 {
  61.                         printf("%-6d  %-8s %-7c %-6c %-6s\n", person[i].num, person[i].name, person[i].sex, person[i].job, person[i].category.position);
  62.                 }
  63.         }
  64. }

复制代码


输出结果:
QQ截图20140605204609.png

提示输出不正常,导致输入也不正常。
但编译可以通过,无error 无warning。
为什么会出现这种情况呢?求大神帮忙开导下。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-5 20:58:55 | 显示全部楼层
本帖最后由 小泉向西流 于 2014-6-5 21:07 编辑

我自己加了一个n, 可以限定输入数据单元的个数。
其他的,依据框图写的。
期望出现视频中的这种效果。(增加部分: 要先选择要输入的结构体元素的个数。其他不变)
QQ截图20140605210355.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-6 14:36:52 | 显示全部楼层
有人来解答吗?
{:1_1:}{:1_1:}
快来大神啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-11 07:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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