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

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
フィボ

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

#1

投稿記事 by フィボ » 14年前

以下が私のソースコードです。

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

non
記事: 1097
登録日時: 14年前

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

#2

投稿記事 by non » 14年前

>E11_2.c:4: error: syntax error before '{' token

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

アバター
沖 滉均
記事: 237
登録日時: 14年前
住所: K県F市

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

#3

投稿記事 by 沖 滉均 » 14年前

まず、構造体の宣言の仕方が間違っています。
他にも問題点は色々ありますが…
構造体についてだけ以下に一例を書きますが、何が間違っているか考えてから見てください。
► スポイラーを表示
There is no royal road to learning.
codeタグで指定できる言語
画像

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

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

#4

投稿記事 by みけCAT » 14年前

直接は関係ありませんが、
コードを貼りつけるときは

コード:

タグを使っていただけるとありがたいです。
よろしくお願いします。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

フィボ

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

#5

投稿記事 by フィボ » 14年前

気をつけます。
お願いですから、エラーの処理や加えるべき点(ソース)をおしえていただけますか?

アバター
bitter_fox
記事: 607
登録日時: 14年前
住所: 大阪府

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

#6

投稿記事 by bitter_fox » 14年前

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

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

コード:

struct s_hoge hoge;

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

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

アバター
沖 滉均
記事: 237
登録日時: 14年前
住所: K県F市

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

#7

投稿記事 by 沖 滉均 » 14年前

フィボ さんが書きました:お願いですから、エラーの処理や加えるべき点(ソース)をおしえていただけますか?
もう見られてないかもしれませんが、念のため
皆さん、別に意地悪で正しいソースコードを全部載せていない訳じゃないのですよ。
しっかり、エラーの原因を理解して直してもらいたいと(少なくとも私は)思っているので
エラーの原因を少しずつ指摘したりヒントを出したりしているのです。
どうしても急ぐから修正したソースが欲しいと言えば…まぁ、書いてくれる方もいるかもしれませんけども…
There is no royal road to learning.
codeタグで指定できる言語
画像

閉鎖

“C言語何でも質問掲示板” へ戻る