Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I gott his error after compiling. I could not find the error, whats the error please?
error:
error: expected expression before ‘{’ token
item[0]={"rice",10,40,30};

Objective-C
#include<stdio.h>
#include<stdlib.h>
#define ITEMLIMIT 7
#define NOOFITEM  3

struct item_info
{
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;
      //int quatityonorder;
 }item[NOOFITEM];

int main()
{
       item[0]={"rice",10,40,30};
       // item[1]={"sugar",10,40,30};
       // item[2]={"soap",10,40,30};
      int choice = 0,i=0,quantity=0;
      char *itemname;
      printf("item name\t quantity\t retailsprice\t wholesaeprice\t");

     for(i=0;i<NOOFITEM;i++)
          {
          printf("%s\t %d\t %f\t %f\t",item[i].itemname,item[i].quantity,item[i].retail,item[i].wholesale);
           }

    printf("enter the item which u want\n");
        scanf("%[^\n]s",itemname);

     for(i=0;i<NOOFITEM;i++)
        {
            if(strcmp(item[i].itemname,itemname)==0);
      {
                   printf("%s is %d the item in our stock",itemname,i);
        }

        puts("*** checking for item availabiity ****");
        if(item[i].quantity<0)
     {

         puts("item is out of stock");
     }

         else if(item[i].quantity>0)
         {
             printf("quantity of %s avalable is %d",itemname,item[i].quantity);
         }

    printf("enter quatity you want");
    scanf("%d", &quantity);
     if("item[i].quantity>=ITEMLIMIT")
 
<pre lang="cs">printf("your should pay %f as wholesale price",item[i].wholesale*item[i].quantity);
    }

     else if(item[i].quantity< ITEMLIMIT)
    {
           printf("your should pay %f as retail price",item[i].retail*item[i].quantity);
            item[i].quantity=item[i].quantity-quantity;
    }

    printf("enter 2 to coninue");
    scanf("%d",&choice);

    if(choice==1)

<pre lang="cs">break;
    else if(choice==2)
        continue;
}
}
Posted

You cannot initialise a structure like that at run time. If you are using constant values then you can get the compiler to set it up by coding:
C++
struct item_info
{
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;
      //int quatityonorder;
}item[NOOFITEM] =
{
    {"rice",10,40,30},
    {"sugar",10,40,30},
    {"soap",10,40,30}
};

But if you want to assign values at run time then you have to do it manually like:
C++
strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;
 
Share this answer
 
Comments
Maciej Los 19-Aug-14 13:55pm    
+5
Maciej Los 19-Aug-14 13:57pm    
What about pointer to the structure?
Richard MacCutchan 19-Aug-14 15:15pm    
What?
Maciej Los 19-Aug-14 15:45pm    
Is it possible to initiate structure via pointer?
item_info *ii;
Richard MacCutchan 20-Aug-14 3:24am    
Sorry, yes of course, you can either use a pointer, incrementing it through the loop, or use the index value. It just takes different syntax, thus:

struct item_info* pInfo = item;
strcpy(pInfo->itemname, "rice");
pInfo->quantity = 10;
// etc
pInfo++; // point to next entry
if you change it so it should compile, but it is not the best way
struct item_info
{
      char *itemname;
      int quantity;
...


You have than the problem that itemname is only a pointer and you need to alloc and free it. And manage it somehow.

I would write an "Setter"

Set(struct *item_info, char*name, ...)
{
 strncpy( item_info->name, sizeof(item_info->name), name);//check length!!!

...
}
 
Share this answer
 
Comments
Jeevan83 19-Aug-14 14:08pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900