**在我添加的新代码中。我不认为有任何问题,但程序在我输入项目代码后停止了。我该怎么做才能解决这个问题?
void cart()
{
int code, amount;
float weight, price, total_weight, total_price1;
char product[20];
switch(code)
{
case 1:
product= "Cement";
weight=20;
price=18;
break;
case 2:
product="Concrete";
weight=30;
price=25;
break;
case 3:
product="Ceramic Tile Floor";
weight=0.1;
price=2.2;
break;
case 4:
product="Foam Insulation";
weight=0.1;
price=2.2;
break;
case 5:
product="Fibre-inforced Cement";
weight=35;
price=50;
break;
case 6:
product="Thick Glass Panel";
weight=20;
price=50;
break;
case 7:
product="Thin Glass Panel";
weight=10.5;
price=30;
break;
case 8:
product="Iron Beam";
weight=5;
price=10;
break;
case 9:
product={"Iron Rod";
weight=1;
price=5;
break;
case 10:
product="Plaster Boards";
weight=10;
price=15;
break;
case 11:
product="Quarry Tiles";
weight=0.5;
price=3;
break;
case 12:
product="Steel Beam";
weight=5;
price=10;
break;
case 13:
product="Wooden Boards";
weight=3;
price=5;
break;
}
printf("\nPlease enter the amount desired:");
scanf("%d", &amount);
total_weight=weight*amount;
total_price1=price*amount;
printf("So far, the total weight and total price in the cart is %f kg & RM%0.1f", total_weight, total_price1);
}
我使用的语言是C. 以上是我的程序的代码(不是整个程序)。问题是:
.c|42|error: incompatible types when assigning to type 'char[20]' from type 'char *'|
此错误适用于所有产品系列。我做的数组错了吗?我想将变量产品设置为字符串,每种情况下产品都不同。我该如何在函数中声明变量product?
另外,我如何循环它以便如果用户想要,他可以将另一个“产品”及其数量添加到购物车。然后,将重量和价格添加到总重量和价格中。也许使用哨兵停止进入产品?
最后,有没有办法简化我的工作?
提前致谢。
以下是编辑过的代码:
void cart()
{
int code, amount;
float weight, price, total_weight, total_price1;
char * product;
printf("\nEnter the code of the desired product:");
scanf("%d", &code);
switch(code)
{
case 1:
strcpy(product, "Cement");
weight=20;
price=18;
break;
case 2:
strcpy(product, "Concrete");
weight=30;
price=25;
break;
case 3:
strcpy(product, "Ceramic Tile Floor");
weight=0.1;
price=2.2;
break;
case 4:
strcpy(product, "Foam Insulation");
weight=0.1;
price=2.2;
break;
case 5:
strcpy(product, "Fibre-Inforced Cement");
weight=35;
price=50;
break;
case 6:
strcpy(product, "Thick Glass Panel");
weight=20;
price=50;
break;
case 7:
strcpy(product, "Thin Glass Panel");
weight=10.5;
price=30;
break;
case 8:
strcpy(product, "Iron Beam");
weight=5;
price=10;
break;
case 9:
strcpy(product, "Iron Rod");
weight=1;
price=5;
break;
case 10:
strcpy(product, "Plaster Board");
weight=10;
price=15;
break;
case 11:
strcpy(product, "Quarry Tiles");
weight=0.5;
price=3;
break;
case 12:
strcpy(product, "Steel Beam");
weight=5;
price=10;
break;
case 13:
strcpy(product, "Wooden Board");
weight=3;
price=5;
break;
}
printf("\nPlease enter the amount desired:");
scanf("%d", &amount);
total_weight=weight*amount;
total_price1=price*amount;
printf("So far, the total weight and total price in the cart is %0.2fkg & RM%0.2f", total_weight, total_price1);
}
但随后出现以下消息:
.c|45|warning: 'product' may be used uninitialized in this function [-Wuninitialized]|
在输入任何代码后,我的程序停止工作。
另外,我是否可以知道如何循环程序,以便用户可以继续输入产品和金额代码,直到他想要停止?
好。所以,我用以下代码修复了它:
char * product="Unknown";
但我的程序仍然停止工作。它不会显示总重量和价格。
答案 0 :(得分:0)
在C中,您无法为char []
分配常量字符串。例如
product= "Cement";
不正确。你应该使用
strcpy(product, "Cement");
答案 1 :(得分:0)
更改
char product[20];
到
const char * product;
答案 2 :(得分:0)
如果要使用字符串操作,请注意初始化的产品变量应足够大,以至少等于数据长度+ 1(对于null char)。我之所以这么说,是因为你宣布了product[20]
,但"Fibre-inforced Cement";
的长度是> 20