本文介绍了在'{'标记之前的预期表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在编译后发现了他的错误。我找不到错误,请问是什么错误?
错误:
错误:预期表达式在'{'标记之前
item [0 ] = {rice,10,40,30};
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};
#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;
}
}
推荐答案
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:
strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;
struct item_info
{
char *itemname;
int quantity;
...
你遇到的问题是itemname只是一个指针,你需要分配并释放它。并以某种方式管理它。
我会写一个Setter
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!!!
...
}
这篇关于在'{'标记之前的预期表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!