在结构数组中查找值

时间:2017-05-20 13:27:23

标签: c arrays struct

我试图检查某个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;};

但收到以下错误:

  

错误C2081:&#39;预订&#39; :正式参数列表中的名称非法

谢谢。

这是我的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;
};

1 个答案:

答案 0 :(得分:-1)

以下是我认为您尝试做的最低限度的工作示例。基于上述评论需要注意的重点是:

  • 您需要在标题中使用与函数声明中相同的变量类型。
  • 您还需要在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;                                                                                                       
};