ページ 11

構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 12:10
by フィボ
以下が私のソースコードです。

#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)
返信お願いします。

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 12:19
by non
>E11_2.c:4: error: syntax error before '{' token

4行目の{の前に文法上のエラーがあります。ってことで、
それ以降のエラーはこれをなおしてから考えましょう。

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 13:47
by 沖 滉均
まず、構造体の宣言の仕方が間違っています。
他にも問題点は色々ありますが…
構造体についてだけ以下に一例を書きますが、何が間違っているか考えてから見てください。
► スポイラーを表示

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 14:02
by みけCAT
直接は関係ありませんが、
コードを貼りつけるときは

コード:

タグを使っていただけるとありがたいです。
よろしくお願いします。

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 19:29
by フィボ
気をつけます。
お願いですから、エラーの処理や加えるべき点(ソース)をおしえていただけますか?

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月21日(火) 19:38
by bitter_fox
フィボ さんが書きました:気をつけます。
お願いですから、エラーの処理や加えるべき点(ソース)をおしえていただけますか?
あのエラーの一部はほとんどが、すでに沖さんが指摘されてる構造体の宣言の間違いから発生していると思いますので、まずその点を改善してください。

次に、calculate_bmi関数とprint_student関数です、これらの関数での各々の構造体の値へのアクセスの方法が間違っています。
構造体へのアクセスは、以下のようにします。

コード:

struct s_hoge hoge;

hoge.hoge_value = foo;
まず上の二点を改善してください。その後、尚もエラーが発生しているようでしたら、改善後のコードとエラーを載せていただきますようお願いします。

[hr][修正・追記]
ほとんど==>一部
print_student関数も同様に間違ってたので、追記しました。

Re: 構造体を用いてBMIを計算したいが、エラーが出ます。

Posted: 2010年12月23日(木) 13:11
by 沖 滉均
フィボ さんが書きました:お願いですから、エラーの処理や加えるべき点(ソース)をおしえていただけますか?
もう見られてないかもしれませんが、念のため
皆さん、別に意地悪で正しいソースコードを全部載せていない訳じゃないのですよ。
しっかり、エラーの原因を理解して直してもらいたいと(少なくとも私は)思っているので
エラーの原因を少しずつ指摘したりヒントを出したりしているのです。
どうしても急ぐから修正したソースが欲しいと言えば…まぁ、書いてくれる方もいるかもしれませんけども…