1

I'm having trouble compiling my code. Every time I compile it I get:

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

I tried using typedef before struct but I got "array size not declared". I can't tell why Item isn't being declared properly. The function is supposed to ask the customer which item they want, and then display the proper data.

#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;
};
1
  • tip: you can use free tools like AStyle to fix the formatting of your code before posting Commented Jul 2, 2016 at 1:07

1 Answer 1

3

Your struct is being used before being defined. Simply move your struct definition to be above your main() function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.