gpt4 book ai didi

c - 在结构数组中查找值

转载 作者:行者123 更新时间:2023-11-30 19:51:26 26 4
gpt4 key购买 nike

我正在尝试检查某个book_name是否在图书馆中。我定义了 struct bookarray 库,例如:

typedef struct Book
{
char name[NAME_LENGTH];
char author[AUTHOR_NAME_LENGTH];
char publisher[PUBLISHER_NAME_LENGTH];
char genre[GENRE_LENGTH];
int year;
int num_pages;
int copies;
} book;

book library[BOOK_NUM];

我正在尝试检查我作为输入收到的书名是否在图书馆中:

int check_existance(char *input, book *library)
{
int i;

for (i=0; i < BOOK_NUM; i++) {
if (strcmp(library[i].name , input))
return TRUE;}

return FALSE;};

但收到以下错误:

error C2081: 'book' : name in formal parameter list illegal

谢谢。

这是我的 main.c 文件:

#include <stdio.h>
#include "define_library.h"
#include "add_book.h"


int main()
{
int opt = 0;

printf("*********************************************\n"
"Welcome to BURLAND national library!\n"
"*********************************************\n"
"Library menu:\n"
"1. Add a book\n"
"2. Take a book\n"
"3. Return a book\n"
"4. Print all library books\n"
"5. Quit\n"
"Please choose the desired option [1-5]:\n");

scanf("%d", &opt);

while (opt < 1 || opt > 5)
{
printf("Invalid number was entered. Please choose again 1-3:\n");
scanf("%d", &opt);
}

switch (opt) {
case 1: printf("1 gever\n"); break;
case 2: printf("2 ahi\n"); break;
case 3: printf("3 kapara\n"); break;
case 4: printf("4 meleh\n"); break;
case 5: printf("5 nasich\n"); break;

default:
printf("We should not get here!\n");
}
return 0;
}

这是define_library.h:

#ifndef DEFINE_LIBRARY_H
#define DEFINE_LIBRARY_H

struct book;

struct library;


int check_existance(char, book);

#endif

这是 define_library.c 文件:

#include <stdio.h>
#include <string.h>


#define BOOK_NUM 50
#define NAME_LENGTH 200
#define AUTHOR_NAME_LENGTH 100
#define PUBLISHER_NAME_LENGTH 50
#define GENRE_LENGTH 50
#define TRUE 1
#define FALSE 0

typedef struct Book
{
char name[NAME_LENGTH];
char author[AUTHOR_NAME_LENGTH];
char publisher[PUBLISHER_NAME_LENGTH];
char genre[GENRE_LENGTH];
int year;
int num_pages;
int copies;
} book;

book library[BOOK_NUM];


int check_existance(char *input, book *library)
{
int i;

for (i=0; i < BOOK_NUM; i++) {
if (strcmp(library[i].name , input))
return TRUE;}

return FALSE;
};

最佳答案

这是我认为您正在尝试做的事情的最小工作示例。根据上述评论,需要注意的要点是:

  • 您需要在 header 中使用与函数声明中相同的变量类型。
  • 您还需要在 main 中包含头文件,以便该函数可见。

编译:gcc main.c Define_library.c

main.c

#include <stdio.h>                                                                                                      
#include <string.h>

#include "define_library.h"

book library[BOOK_NUM];

int main()
{
int opt = 0;

printf("*********************************************\n"
"Welcome to BURLAND national library!\n"
"*********************************************\n"
"Library menu:\n"
"1. Add a book\n"
"2. Take a book\n"
"3. Return a book\n"
"4. Print all library books\n"
"5. Quit\n"
"Please choose the desired option [1-5]:\n");

scanf("%d", &opt);

while (opt < 1 || opt > 5)
{
printf("Invalid number was entered. Please choose again 1-3:\n");
scanf("%d", &opt);
}

switch (opt) {
case 1: printf("1 gever\n"); break;
case 2: printf("2 ahi\n"); break;
case 3: printf("3 kapara\n"); break;
case 4: printf("4 meleh\n"); break;
case 5: printf("5 nasich\n"); break;

default:
printf("We should not get here!\n");
}

strcpy(library[2].name,"abc");

int book_present = check_existence("abc",library);
printf("book present: %d\n",book_present);

return 0;
}

define_library.h

#ifndef DEFINE_LIBRARY_H                                                                                                
#define DEFINE_LIBRARY_H

#define BOOK_NUM 50
#define NAME_LENGTH 200
#define AUTHOR_NAME_LENGTH 100
#define PUBLISHER_NAME_LENGTH 50
#define GENRE_LENGTH 50
#define TRUE 1
#define FALSE 0

typedef struct Book
{
char name[NAME_LENGTH];
char author[AUTHOR_NAME_LENGTH];
char publisher[PUBLISHER_NAME_LENGTH];
char genre[GENRE_LENGTH];
int year;
int num_pages;
int copies;
} book;

int check_existence(char *, book *);

#endif

define_library.c

#include <stdio.h>                                                                                                      
#include <string.h>

#include "define_library.h"

int check_existence(char *input, book *library)
{
for (int i=0; i < BOOK_NUM; i++) {
if (strcmp(library[i].name , input))
return TRUE;
}
return FALSE;
};

关于c - 在结构数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44086444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com