0

**在我添加的新代码中。我不认为有任何问题,但是在我输入项目代码后程序停止了。我该怎么做才能解决这个问题???

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 *'|

此错误适用于产品的所有行。我做错数组了吗?我想将变量产品设置为字符串,并且每种情况下产品都不同。我应该如何在函数中声明变量产品?

另外,我如何循环它,以便如果用户想要,他可以将另一个“产品”及其数量添加到购物车中。然后,将重量和价格添加到总重量和价格中。也许使用哨兵停止输入产品?

最后,有没有办法简化我所做的事情?

提前致谢。

这是编辑后的代码:

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

但我的程序仍然停止工作。它不会显示总重量和价格。

4

3 回答 3

0

在 C 中,您不能将常量字符串分配给char []. 例如

product= "Cement";

是不正确的。你应该使用

strcpy(product, "Cement");
于 2014-02-17T22:10:32.483 回答
0

改变

char product[20];

const char * product;
于 2014-02-17T22:13:33.613 回答
0

如果要使用字符串操作,请注意将产品变量初始化为足够大以至少等于数据长度 + 1(对于空字符)。我这么说是因为你已经声明product[20]但长度"Fibre-inforced Cement";> 20

于 2014-02-17T22:17:20.340 回答