2016-07-02 2 views
1

コードのコンパイルに問題があります。私はそれをコンパイルするたびに私が取得:エラーが不完全な要素タイプの構造体

rec5.c: In function âmainâ: 
    rec5.c:7:17: error: array type has incomplete element type 
struct Item cart[3]; 
      ^
    rec5.c:8:16: error: array type has incomplete element type 
struct Item book[4]; 
      ^
    rec5.c:9:16: error: array type has incomplete element type 
struct Item clothing[5]; 
      ^
    rec5.c:10:16: error: array type has incomplete element type 
struct Item sports[6]; 
      ^
    rec5.c:20:5: error: expected â;â before âprintfâ 
printf("%s %d %d", book.name, book.price, book.quantity); 
^ 
    rec5.c:24:6: error: expected â;â before âprintfâ 
printf("%s %d %d", clothing.name, clothing.price, clothing.quantity); 
^ 
    rec5.c:28:6: error: expected â;â before âprintfâ 
printf("%s %d %d", sports.name, sports.price, sports.quantity); 

私は構造体の前にtypedefを使用してみましたが、私は「配列サイズが宣言されていない」です。なぜItemが正しく宣言されていないのかわかりません。この関数は、顧客に希望する項目を尋ね、適切なデータを表示することになっています。

#include <stdio.h> 

int main(){ 
    struct Item cart[3]; 
    struct Item book[4]; 
    struct Item clothing[5]; 
    struct Item sports[6]; 

    book.name = "harry potter"; 
    book.price == "$100"; 
    clothing.name = "shirt"; 
    clothing.price == "$15"; 
    sports.name = "football"; 
    sports.price = "20"; 

    scanf("enter Item %c", cart.type); 

    if (cart.type == "book"){ 
     scanf("please enter quantity %d", book.quantity) 
     printf("%s %d %d", book.name, book.price, book.quantity); 
    } 
    if (cart.type == "clothing"){ 
     scanf("please enter quantity %d", clothing.quantity) 
     printf("%s %d %d", clothing.name, clothing.price, clothing.quantity); 
    } 
    if (cart.type == "sports"){ 
     scanf("please enter quantity %d",sports.quantity) 
     printf("%s %d %d", sports.name, sports.price, sports.quantity); 
    } 
} 

struct Item 
{ 
    char *type; 
    char *name; 
    double price; 
    double quantity; 
}; 
+0

ヒント:投稿する前にコードの書式を修正するために、AStyleのような無料のツールを使用できます –

答えて

3

お客様のstructは、定義される前に使用されています。 struct定義をmain()機能の上に移動するだけです。

関連する問題