#include <stdio.h>
#include <string.h>
struct chat
{
char type[10];
}loai;
struct goods
{
char name[20];
int code;
float price;
int how_many;
struct chat loai;
}biens[20];
void input_goods(struct goods, int n);
void output_goods(struct goods, int n);
float all_price(struct goods, int n);
int main(int argc, char *argv[])
{
struct goods biens[20];
int n;
printf("So luong mat hang co trong kho: ");
scanf("%d", &n);
input_goods(biens[20], n);
output_goods(biens[20], n);
printf("\n Tong gia tri cua kho hang la: ");
printf("\n %f", all_price(biens[20], n));
}
void input_goods(struct goods biens, int n)
{
int i;
for(i=0; i<n; i++)
{
printf("\nMat hang thu %d: ", i+1);
printf("\n\tName: ");
gets(biens[i].name); //chỗ này bị lỗi//
printf("\n\tCode: ");
scanf("%d", &biens[i].code);
printf("\n\tPrice: ");
scanf("%f", &biens[i].price);
printf("\t");
gets(biens[i].loai.type);
printf("\n\tHow many?: ");
scanf("%d", &biens[i].how_many);
printf("\t");
gets(biens[i].loai.type);
}
}
void output_goods(struct goods biens, int n)
{
int i;
printf("\nDanh sach cac mat hang co trong kho: ");
printf("\n------------------MENU------------------");
for(i=0; i<n; i++)
{
printf("\n%d. %s", i+1, biens.name);
}
printf("\n");
scanf("%d", &i);
if(i)
{
printf("\nName: %s", biens[i].name);
printf("\nCode: %d", biens[i].code);
printf("\nPrice: %f", biens[i].price);
printf("\t%s", biens[i].loai.type);
printf("\nHow many: %d", biens[i].how_many);
printf("\t%s", biens[i].loai.type);
}
}
float all_price(struct goods biens, int n)
{
int i;
float all=0;
for(i=0; i<n; i++)
{
all+=biens[i].price;
}
return all;
}
cho em hỏi, tại sao khi chạy chương trình này thì chỉ câu lệnh gets() đầu tiên bị máy bỏ qua ??
số lượng mặt hàng có trong kho: 3
mat hang thu 1:
name: //cho nay bi may bo qua, hok nhap duoc//
code:_
và còn vấn đề nữa là: khi em coding: biens[i].name (đây là cách truy cập phần tử cấu trúc mảng mà e được biết), thế mà khi compile nó xuất hiện lỗi.
[Error] no match for 'operator[]' (operand types are 'goods' and 'int')
Dù vậy chương trình vẫn execute được ??