我想通过C语言制作收银员计划。我使用结构进行记录,但问题是当我尝试按项目detail[i].name
的定义名称和价格detail.[i].price
显示付款收据进行循环时,我收到此消息
[错误]来自' char *'的无效转换去#char;' [-fpermissive]
这是我的剧本
#include<stdio.h>
#include <stdlib.h>
struct item{
char name[10];
int price;
int barcode;
};
struct item detail[10]={
"item1",10,1,
"item2",20,2,
"item3",30,3,
"item4",40,4,
"item1",50,5,
"item2",60,6,
"item3",70,7,
"item4",80,8,
"item3",90,9,
"item4",100,10
};
int main(){
int ibarcode[10];int qty[10];char b[10];int price[10];int ju[10];int tot[10];
int j,i,k,grand;
char a;
printf("Cashier program\n");
for(j=0;j<10;j++){
printf("enter barcode : ");scanf("%d",&ibarcode[j]);
for(i=0;i<10;i++){
if(ibarcode[j]==detail[i].barcode){
printf("item name: %s\n",detail[i].name);
printf("price : %d\n",detail[i].price);
printf("Quantity : ");scanf("%d",&qty[j]);
tot[j]=detail[j].price*qty[j];
}
if(ibarcode[j] > 10){
printf("Barcode not valid\n");
j--;
break;
}
}
printf("\nDo you want to buy again? [Y/N] = ");scanf("%s", &a);
b[j] = detail[i].name;
ju[j] = detail[i].price;
if(a=='Y'||a=='y'){
continue;
} else {
break;
}
}
grand = 0; system("cls");
printf("\n name Kasir = Addzifi Moch G\n");
printf(" Tanggal = 03 januari 2017\n");
printf(" Jam = 14:05 WIB\n\n");
printf("+-------------------------------------------------------------------------------------------------+\n");
printf("| Barcode | name item\t\t\t| price \t\t| Quantity\t| Total |\n");
printf("+-------------------------------------------------------------------------------------------------+\n");
for(k=0; k<=j; k++){
grand += tot[k];
printf("| %d \t | %s\t | %d\t\t | %d\t\t\t| %d |\n", ibarcode[k], b[k], ju[k], qty[k], tot[k]);
}
printf("+-------------------------------------------------------------------------------------------------+\n");
printf("|\t\t\t\t\t\t\t Total Yang Harus Dibayarkan = %d |\n", grand);
printf("+-------------------------------------------------------------------------------------------------+\n");
}
答案 0 :(得分:4)
我敢打赌,就行了
b[j] = detail[i].name;
这是因为b
是一个字符数组,name
是一个字符串,所以你要分配给b[j]
,一个字符串,一个字符串。
使用strcpy
:
strcpy(b, detail[i].name);