2016-07-02 6 views
1

내 코드를 컴파일하는 데 문제가 있습니다. 나는 그것을 컴파일 할 때마다 내가 얻을 :불완전한 요소 유형 struct

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); 

내가 구조체 전에 형식 정의 사용하여 시도하지만 "배열 크기가 선언되지"있어요. 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() 기능 위에 올려 놓기 만하면됩니다.

관련 문제