当前位置:编程学习 > C/C++ >>

C语言结构体与共用体小问题

写一个代码来表示这个表格。如果职业是老师 就输入职务,如果是学生就输入班级

请问 代码哪里有问题

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct information

{

int num;

char name[10];

char sex;

char job;

union
 {
  int  banji;

  char zhiwu;

 }duoxuan;

}person[2];

int main( void )

{

int i;

for( i=0;i<2;i++ )

 {

 scanf( "%d%s%c%s",&person[i].num,person[i].name,&person[i].sex,person[i].job );//貌似输出的是字符串 可以不加&的吧

 if ( strcmp( person[i].job,"teacher")==0 )   //判断需要输入的是职务还是班级

   scanf( "%s",person[i].duoxuan.zhiwu );

 else if ( strcmp(person[i].job,"student")==0 )

   scanf("%d",&person[i].duoxuan.banji);

 else printf("error.");

 }

  for(i=0;i<2;i++)

  {

  if ( strcmp(person[i].job,"student")==0 )

   printf("%d  %S  %c  %d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.banji);

  else if ( strcmp( person[i].job,"teacher")==0 )

   printf("%d  %s  %c  %s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.zhiwu);

  }

 getch();

 return 0;
}

在结构体的定义中出现一个共用体 帮忙看看代码哪里错了。


 

追问:想问

1 这句 错在哪里啊 printf("zhiwu:");
   scanf( "%s",&person[i].duoxuan.zhiwu );

2 另外 我感觉应该在共用体中定义结构为字符串。所以我在新的代码里修改了,但是还是程序无法运行,麻烦你帮忙看看

 printf("zhiwu:");
   scanf( "%s",&person[i].duoxuan.zhiwu );

 

 

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct information

{

int num;

char name[10];

char sex;

char job[10]; /////////// 用了字符数组

union
 {
  int  banji;

  char zhiwu[10];/////////////用了字符数组

 }duoxuan;

}person[2];

int main( void )

{

int i;

for( i=0;i<2;i++ )

 {

 scanf( "%d",&person[i].num );
 scanf("%s",person[i].name);
 fflush(stdin);//////////

 scanf("%c",&person[i].sex);
 scanf("%s",person[i].job);
 fflush(stdin);/////////////////

 

 if ( strcmp( person[i].job,"teacher")==0 )  

   scanf( "%s",person[i].duoxuan.zhiwu );////////////////

 else if ( strcmp(person[i].job,"student")==0 )

   scanf("%d",&person[i].duoxuan.banji);

 else printf("error.");

 }

  for(i=0;i<2;i++)

  {

  if ( strcmp(person[i].job,"student")==0 )

   printf("%d  %S  %c  %d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.banji);

  else if ( strcmp( person[i].job,"teacher")==0 )

   printf("%d  %s  %c  %s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.zhiwu);

  }

 getch();

 return 0;
}

 


答案:scanf这样用本身就是有问题的,另外就是char job这个定义有问题。修改部分已经加粗。

 

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct information

{
 
 int num;
 
 char name[10];
 
 char sex;
 
 char job[10];
 
 union
 {
  int  banji;
  
  char zhiwu;
  
 }duoxuan;
 
}person[2];

int main( void )

{
 
 int i;
 
 for( i=0;i<2;i++ )
  
 {
  printf("num:");
  scanf("%d",&person[i].num);
  printf("name:");
  scanf("%s",person[i].name);
  fflush(stdin);
  printf("sex:");
  scanf("%c",&person[i].sex);
  printf("job:");
  scanf("%s",person[i].job );//貌似输出的是字符串 可以不加&的吧
  fflush(stdin);
  
  if ( strcmp( person[i].job,"teacher")==0 )   //判断需要输入的是职务还是班级
  {
   printf("zhiwu:");
   scanf( "%s",&person[i].duoxuan.zhiwu );
  }
  
  else if ( strcmp(person[i].job,"student")==0 )
  {
   printf("banji:");
   scanf("%d",&person[i].duoxuan.banji);
  }
  
  else printf("error.");
  fflush(stdin);
  
 }
 
 for(i=0;i<2;i++)
  
 {
  
  if ( strcmp(person[i].job,"student")==0 )
   
   printf("%d  %S  %c  %d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.banji);
  
  else if ( strcmp( person[i].job,"teacher")==0 )
   
   printf("%d  %s  %c  %s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].duoxuan.zhiwu);
  
 }
 
 getch();
 
 return 0;
}

看到char job这是一个字符变量,又看到 if ( strcmp(person[i].job,"student")==0 )。job应该定义为数组。其他没有看了。
为什么每次认真看,找出问题你都不采纳?都关闭么?

scanf( "%d%s%c%s",&person[i].num,person[i].name,&person[i].sex,person[i].job );

scanf()只处理整数和字符,你输入的字符串不能处理

上一个:求高人给咱做个C语言编程
下一个:C语言软件内存问题,高手来。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,