By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

expected expression before { token

P: 9
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};


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define ITEMLIMIT 7
  4. #define NOOFITEM  3
  5.  
  6. struct item_info
  7. {
  8.       char itemname[15];
  9.       int quantity;
  10.       float retail;
  11.       float wholesale;
  12.       //int quatityonorder;
  13.  }item[NOOFITEM];
  14.  
  15. int main()
  16. {
  17.        item[0]={"rice",10,40,30};
  18.        // item[1]={"sugar",10,40,30};
  19.        // item[2]={"soap",10,40,30}; 
  20.       int choice = 0,i=0,quantity=0;
  21.       char *itemname;
  22.       printf("item name\t quantity\t retailsprice\t wholesaeprice\t");
  23.  
  24.      for(i=0;i<NOOFITEM;i++)
  25.           {
  26.           printf("%s\t %d\t %f\t %f\t",item[i].itemname,item[i].quantity,item[i].retail,item[i].wholesale);
  27.            }
  28.  
  29.     printf("enter the item which u want\n");
  30.         scanf("%[^\n]s",itemname);
  31.  
  32.      for(i=0;i<NOOFITEM;i++)
  33.         {
  34.             if(strcmp(item[i].itemname,itemname)==0);
  35.       {
  36.                    printf("%s is %d the item in our stock",itemname,i);
  37.         }
  38.  
  39.  
  40.         puts("*** checking for item availabiity ****");
  41.         if(item[i].quantity<0)
  42.      {
  43.  
  44.          puts("item is out of stock");
  45.      }
  46.  
  47.          else if(item[i].quantity>0)
  48.          {
  49.              printf("quantity of %s avalable is %d",itemname,item[i].quantity);
  50.          }
  51.  
  52.  
  53.  
  54.     printf("enter quatity you want");
  55.     scanf("%d", &quantity);
  56.      if("item[i].quantity>=ITEMLIMIT")
  57.     {
  58.           printf("your should pay %f as wholesale price",item[i].wholesale*item[i].quantity);
  59.     }
  60.  
  61.      else if(item[i].quantity< ITEMLIMIT)
  62.     {
  63.            printf("your should pay %f as retail price",item[i].retail*item[i].quantity);
  64.             item[i].quantity=item[i].quantity-quantity;
  65.     }
  66.  
  67.     printf("enter 2 to coninue");
  68.     scanf("%d",&choice);
  69.  
  70.     if(choice==1)
  71.           break;
  72.     else if(choice==2)
  73.         continue; 
  74. }
  75. }
Aug 19 '14 #1

✓ answered by horace1

you cannot assign the elements of a struct so
Expand|Select|Wrap|Line Numbers
  1. item[0]={"rice",10,40,30};
  2.  
you can assign each element so
Expand|Select|Wrap|Line Numbers
  1.   item[0].quantity=10;
  2.   item[0].retail=40;
  3.  
or when you declare you can give it initial values
Expand|Select|Wrap|Line Numbers
  1.     struct item_info
  2.     {
  3.           char itemname[15];
  4.           int quantity;
  5.           float retail;
  6.           float wholesale;
  7.           //int quatityonorder;
  8.      }item[NOOFITEM]={{"rice",10,40,30},{},{}};;
  9.  

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you cannot assign the elements of a struct so
Expand|Select|Wrap|Line Numbers
  1. item[0]={"rice",10,40,30};
  2.  
you can assign each element so
Expand|Select|Wrap|Line Numbers
  1.   item[0].quantity=10;
  2.   item[0].retail=40;
  3.  
or when you declare you can give it initial values
Expand|Select|Wrap|Line Numbers
  1.     struct item_info
  2.     {
  3.           char itemname[15];
  4.           int quantity;
  5.           float retail;
  6.           float wholesale;
  7.           //int quatityonorder;
  8.      }item[NOOFITEM]={{"rice",10,40,30},{},{}};;
  9.  
Aug 19 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.