構造体を用いてBMIを計算したいが、エラーが出ます。
Posted: 2010年12月21日(火) 12:10
以下が私のソースコードです。
#include <stdio.h>
#define N_STUDENTS 5
STUDENT{
int no;
double height;
double weight;
double bmi;
}person[N_STUDENTS] ;
void calculate_bmi(STUDENT data[]);
void print_student(STUDENT student);
int main(void)
{
STUDENT person[N_STUDENTS]={
{1001, 160.5, 53.1},
{1002, 173.8, 79.2},
{1003, 169.7, 65.3},
{1004, 175.3, 72.7},
{1005, 187.2, 98.9},
}
int i;
calculate_bmi(person);
for (i=0; i<N_STUDENTS ;i++)
{
print_student(person);
}
return 0;
}
void calculate_bmi(STUDENT data[])
{
int i;
for (i=0; i<N_STUDENTS ;i++)
{
bmi=weight/(height*height);
}
}
void print_student(STUDENT student)
{
printf("%4d %5.1f %4.1f %4.1f\n",no,height,weight,bmi);
}
下記のエラーが出ましたが、意味がわかりずらく治せません。
E11_2.c:4: error: syntax error before '{' token
E11_2.c:12: error: parse error before '}' token
E11_2.c:12: warning: data definition has no type or storage class
E11_2.c:17: error: parse error before "data"
E11_2.c:18: error: parse error before "student"
E11_2.c: In function `main':
E11_2.c:22: error: `STUDENT' undeclared (first use in this function)
E11_2.c:22: error: (Each undeclared identifier is reported only once
E11_2.c:22: error: for each function it appears in.)
E11_2.c:22: error: parse error before "person"
E11_2.c: At top level:
E11_2.c:31: warning: parameter names (without types) in function declaration
E11_2.c:31: error: conflicting types for 'calculate_bmi'
E11_2.c:17: error: previous declaration of 'calculate_bmi' was here
E11_2.c:31: error: conflicting types for 'calculate_bmi'
E11_2.c:17: error: previous declaration of 'calculate_bmi' was here
E11_2.c:31: warning: data definition has no type or storage class
E11_2.c:33: error: parse error before "for"
E11_2.c:41: error: parse error before "data"
E11_2.c:42: error: conflicting types for 'calculate_bmi'
E11_2.c:31: error: previous declaration of 'calculate_bmi' was here
E11_2.c:42: error: conflicting types for 'calculate_bmi'
E11_2.c:31: error: previous declaration of 'calculate_bmi' was here
E11_2.c:51: error: parse error before "student"
E11_2.c: In function `print_student':
E11_2.c:53: error: `no' undeclared (first use in this function)
返信お願いします。
#include <stdio.h>
#define N_STUDENTS 5
STUDENT{
int no;
double height;
double weight;
double bmi;
}person[N_STUDENTS] ;
void calculate_bmi(STUDENT data[]);
void print_student(STUDENT student);
int main(void)
{
STUDENT person[N_STUDENTS]={
{1001, 160.5, 53.1},
{1002, 173.8, 79.2},
{1003, 169.7, 65.3},
{1004, 175.3, 72.7},
{1005, 187.2, 98.9},
}
int i;
calculate_bmi(person);
for (i=0; i<N_STUDENTS ;i++)
{
print_student(person);
}
return 0;
}
void calculate_bmi(STUDENT data[])
{
int i;
for (i=0; i<N_STUDENTS ;i++)
{
bmi=weight/(height*height);
}
}
void print_student(STUDENT student)
{
printf("%4d %5.1f %4.1f %4.1f\n",no,height,weight,bmi);
}
下記のエラーが出ましたが、意味がわかりずらく治せません。
E11_2.c:4: error: syntax error before '{' token
E11_2.c:12: error: parse error before '}' token
E11_2.c:12: warning: data definition has no type or storage class
E11_2.c:17: error: parse error before "data"
E11_2.c:18: error: parse error before "student"
E11_2.c: In function `main':
E11_2.c:22: error: `STUDENT' undeclared (first use in this function)
E11_2.c:22: error: (Each undeclared identifier is reported only once
E11_2.c:22: error: for each function it appears in.)
E11_2.c:22: error: parse error before "person"
E11_2.c: At top level:
E11_2.c:31: warning: parameter names (without types) in function declaration
E11_2.c:31: error: conflicting types for 'calculate_bmi'
E11_2.c:17: error: previous declaration of 'calculate_bmi' was here
E11_2.c:31: error: conflicting types for 'calculate_bmi'
E11_2.c:17: error: previous declaration of 'calculate_bmi' was here
E11_2.c:31: warning: data definition has no type or storage class
E11_2.c:33: error: parse error before "for"
E11_2.c:41: error: parse error before "data"
E11_2.c:42: error: conflicting types for 'calculate_bmi'
E11_2.c:31: error: previous declaration of 'calculate_bmi' was here
E11_2.c:42: error: conflicting types for 'calculate_bmi'
E11_2.c:31: error: previous declaration of 'calculate_bmi' was here
E11_2.c:51: error: parse error before "student"
E11_2.c: In function `print_student':
E11_2.c:53: error: `no' undeclared (first use in this function)
返信お願いします。