首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

书下例子有有关问题,不知怎么修改,

2012-09-13 
书上例子有问题,不知如何修改,求助!#includestdio.h#includeconio.h#includestring.hunion conditio

书上例子有问题,不知如何修改,求助!
#include<stdio.h>
#include<conio.h>
#include<string.h>
union condition
{float score[5]
 char T_course[80];
};
struct person
{ int num;
  char name[20];
  char status;
  union conditionstate;
};
main()
{ int i,j;
  struct person personnel[30];
  for (i=0;i<30;i++)
  { printf("\nEnter num:");
  scanf("%d",&personnel[i].num);
printf("\nEnter name:");
scanf("%s",personnel[i].name);
printf("\nEnterstatus:");
scanf("%c",&personnel[i].status);
if(personnel[i].status=='s')
{ printf("\nEnter 5:scores:");
for (j=0;j<5;j++)
scanf("%f",&personnel[i].state.score[j]);
}
else
{ printf("\nEnter terch courses:");
gets(personnel[i].state.T_course);
}
  }
  for(i=0;i<30;i++)
{ printf("\n%d,%s",personnel[i].num,personnel[i].name);
printf(",%c\n",personnel[i].status);
if(personnel[i].status=='t')
printf("%s",personnel[i].state.T_course);
else
for(j=0;j<5;j++)
printf("%6.1f",personnel[i].state.score[j]);
}
}


--------------------Configuration: helloc - Win32 Debug--------------------
Compiling...
helloc.cpp
D:\VC语言\helloc\helloc.cpp(6) : error C2144: syntax error : missing ';' before type 'char'
D:\VC语言\helloc\helloc.cpp(27) : error C2039: 'state' : is not a member of 'person'
  D:\VC语言\helloc\helloc.cpp(9) : see declaration of 'person'
D:\VC语言\helloc\helloc.cpp(27) : error C2228: left of '.score' must have class/struct/union type
D:\VC语言\helloc\helloc.cpp(31) : error C2039: 'state' : is not a member of 'person'
  D:\VC语言\helloc\helloc.cpp(9) : see declaration of 'person'
D:\VC语言\helloc\helloc.cpp(31) : error C2228: left of '.T_course' must have class/struct/union type
D:\VC语言\helloc\helloc.cpp(38) : error C2039: 'state' : is not a member of 'person'
  D:\VC语言\helloc\helloc.cpp(9) : see declaration of 'person'
D:\VC语言\helloc\helloc.cpp(38) : error C2228: left of '.T_course' must have class/struct/union type
D:\VC语言\helloc\helloc.cpp(41) : error C2039: 'state' : is not a member of 'person'
  D:\VC语言\helloc\helloc.cpp(9) : see declaration of 'person'
D:\VC语言\helloc\helloc.cpp(41) : error C2228: left of '.score' must have class/struct/union type
D:\VC语言\helloc\helloc.cpp(43) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

helloc.exe - 1 error(s), 0 warning(s)


[解决办法]
和楼上一样。float score[5]还少个分号。

C/C++ code
#include<stdio.h>#include<conio.h>#include<string.h>union condition{    float score[5];    char T_course[80];};struct person{     int num;    char name[20];    char status;    union condition state;};int main(){     int i,j;    struct person personnel[30];    for (i=0;i<30;i++)    {         printf("\nEnter num:");        scanf("%d",&personnel[i].num);        printf("\nEnter name:");        scanf("%s",personnel[i].name);        printf("\nEnterstatus:");        scanf("%c",&personnel[i].status);        if(personnel[i].status=='s')        {             printf("\nEnter 5:scores:");            for (j=0;j<5;j++)            scanf("%f",&personnel[i].state.score[j]);        }        else        {             printf("\nEnter terch courses:");            gets(personnel[i].state.T_course);        }    }    for(i=0;i<30;i++)    {        printf("\n%d,%s",personnel[i].num,personnel[i].name);        printf(",%c\n",personnel[i].status);        if(personnel[i].status=='t')        {            printf("%s",personnel[i].state.T_course);        }        else        {            for(j=0;j<5;j++)            printf("%6.1f",personnel[i].state.score[j]);        }    }    return 0;} 


[解决办法]
#include <stdio.h>
#include <conio.h>
#include <string.h>
union condition
{float score[5];//少分号
char T_course[80];
};
struct person
{ int num;
char name[20];
char status;
union condition state;//condition state间加空格
};
void main() // 少个类型 void或int 等等
{ int i, j;
struct person personnel[30];
for (i = 0;i < 30;i++)
{ printf("\nEnter num:");
scanf("%d", &personnel[i].num);
printf("\nEnter name:");
scanf("%s", personnel[i].name);
printf("\nEnterstatus:");
scanf("%c", &personnel[i].status);
if (personnel[i].status == 's')
{ printf("\nEnter 5:scores:");
for (j = 0;j < 5;j++)
scanf("%f", &personnel[i].state.score[j]);
}
else
{ printf("\nEnter terch courses:");
gets(personnel[i].state.T_course);
}
}
for (i = 0;i < 30;i++)
{ printf("\n%d,%s", personnel[i].num, personnel[i].name);
printf(",%c\n", personnel[i].status);
if (personnel[i].status == 't')
printf("%s", personnel[i].state.T_course);
else
for (j = 0;j < 5;j++)
printf("%6.1f", personnel[i].state.score[j]);
}
}

热点排行