30/09/2018, 16:24

Lỗi hàm gets() & mảng cấu trúc, lỗi subscripted value is neither array nor pointer nor vector


#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("
 Tong gia tri cua kho hang la: ");
	printf("
 %f", all_price(biens[20], n));
}
void input_goods(struct goods biens, int n)
{
	int i;
	for(i=0; i<n; i++)
	{
		printf("
Mat hang thu %d: ", i+1);
		printf("
	Name: ");
		gets(biens[i].name); //chỗ này bị lỗi//
		printf("
	Code: ");
		scanf("%d", &biens[i].code);
		printf("
	Price: ");
		scanf("%f", &biens[i].price);
		printf("	");
		gets(biens[i].loai.type);
		printf("
	How many?: ");
		scanf("%d", &biens[i].how_many);
		printf("	");
		gets(biens[i].loai.type);
	}
}
void output_goods(struct goods biens, int n)
{
	int i;
	printf("
Danh sach cac mat hang co trong kho: ");
	printf("
------------------MENU------------------");
	for(i=0; i<n; i++)
	{
		printf("
%d. %s", i+1, biens.name);
	}
	printf("
");
	scanf("%d", &i);
	if(i)
	{
		printf("
Name: %s", biens[i].name);
		printf("
Code: %d", biens[i].code);
		printf("
Price: %f", biens[i].price);
		printf("	%s", biens[i].loai.type);
		printf("
How many: %d", biens[i].how_many);
		printf("	%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 ??

Nguyễn Minh Dũng viết 18:35 ngày 30/09/2018

Cách truyền mảng của em vào trong hàm bị sai cú pháp rồi. Hiện giờ em đang truyền mảng như thế này:

void input_goods(struct goods, int n);

Với cấu trúc như thế này thì C hiểu là em truyền vào một struct chứ không phải là một mảng struct. Để có thể truyền mảng struct em phải truyền như sau

void input_goods(struct goods[], int n);

Ở dưới đây là code anh đã sửa toàn bộ cách em gửi mảng struct vào.

#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, n);
    output_goods(biens, n);
    printf("\n Tong gia tri cua kho hang la: ");
    printf("\n %f", all_price(biens, 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);
        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[i].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;
}
Programmer Riot viết 18:36 ngày 30/09/2018

còn cái lỗi câu lệnh gets() đầu tiên bị máy tự nhiên bỏ qua, anh giải thích dùm e lun

Nguyễn Minh Dũng viết 18:38 ngày 30/09/2018

Anh không nghĩ là nó bỏ qua, nó đã báo error thì tức là nó đã ngắt chương trình lại. Việc em “cảm giác” là nó đã bỏ qua theo anh nghĩ em đang làm Visual Studio. Và Visual Studio nó có một cái tính năng là sẽ chạy lại chương trình ở lần compile thành công trước.

Nên anh nghĩ rằng code của em thực ra đang chạy code của lần compile trước. Về việc bỏ qua lỗi, thì compiler của C không bao giờ bỏ qua lỗi, nó chỉ bỏ qua warning, tức là cảnh báo.

Để cho chắc ăn, em thử compile lại, xem compiling logs thiệt là kỹ, để xem nó có ghi là đã compile thành công hay không. Thông thường nó sẽ ghi

compile successfully
viết 18:37 ngày 30/09/2018

bạn không in được tên đầu tiên do khi bạn scanf số lượng mặt hàng trong kho hàm scanf k lấy dấu enter nên cái dấu enter bị lưu lại cho bộ đêm(mình không nhớ chính xác cách nói) và bị tính vào hàm input ở ngay sau và hàm gets() trong input lấy nên tên của sản phẩm đầu tiên là enter luôn bạn có thể sửa code như dưới ( thêm dòng while(getchar() != ‘\n’); nagy sau dòng scanf trong hàm main )

#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);
    while(getchar() != '\n' );
    input_goods(biens, n);
    output_goods(biens, n);
    printf("\n Tong gia tri cua kho hang la: ");
    printf("\n %f", all_price(biens, 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[i].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;
}
Programmer Riot viết 18:33 ngày 30/09/2018

thank bạn, mình hiểu rồi

Hoan Sò viết 18:40 ngày 30/09/2018

sao mình chạy n=1 mà khi xuất ra màn hình thì nó toàn giá trị rách vậy, mà nhân tiện cho mình hỏi struct (loai) để làm gì vậy.

Bài liên quan
0