想问一下我哪一点做的不对呀(函数要求输出给定出版社的所有书本信息

函数接口定义:

void print_press_books(Library lb)

参数lbLibrary类型的变量,你需要完成这个函数的实现。

裁判测试程序样例:

#include <iostream>

#include <string.h>

#include <string>

#include <stdlib.h>

#include <iomanip> #define MAXBKS 1000 // 最多1000本书

using namespace std;

// 测试程序已完成Book结构体声明

struct Book
{
    int num;
    char name[101];
    char press[101];
    int date;
    float price;
};

// 测试程序已完成Library结构体声明

struct Library
{
    int id;
    char name[101];
    int n;
    struct Book bks[MAXBKS];
};

// 测试程序已完测试函数的定义

/* 请按要求在下面完成函数定义 */

// 定义函数 print_press_books

// 主函数

int main()

  Library lb={1,"shanghai",0};

// 测试程序会调用 add_books 函数添加书本

 print_press_books(lb); return 0;

}

输入样例1:

假定本次测试,有满足条件的书2本。

请输入要查找的出版社名:
aaaa

输出样例1:

假设目前有2本书是aaaa出版社出版的。

2本图书信息如下:
书号    书名    出版社    出版日期    价格
1    a    aaaa    20211201    99.00
2    b    aaaa    20211101    90.00

输入样例2:

假定本次测试,没有满足条件的书。

请输入要查找的出版社名:
nnnn

输出样例2:

没找到满足条件的图书!

 

然后这个是我写的代码:

void print_press_books(Library lb)
{
//假定本次测试,有满足条件的书2本。请输入要查找的出版社名:aaaa
      int count=0;
      cout<<"请输入要查找的出版社名:"<<endl;
      string press1;
      cin>>press1;
      for(int i=0;i<lb.n;i++)
      {
          if(lb.bks[i].press==press1)
              count++;
      }
      if(count==0) cout<<"没找到满足条件的图书!"<<endl;
      else
      {
          cout<<count<<"本图书信息如下:"<<endl;
          cout<<"书号"<<'\t'<<"书名"<<'\t'<<"出版社"<<'\t'<<"出版日期"<<'\t'<<"价格"<<endl;
          for(int i=1;i<=lb.n;i++)
          {
              if(lb.bks[i].press==press1)
              {
                  cout<<lb.bks[i].num<<'\t'<<lb.bks[i].name<<'\t'<<lb.bks[i].press<<'\t'<<lb.bks[i].date<<'\t'<<lb.bks[i].price<<endl;
              }
          }
      }

}

 

第二个for从0开始吧


 第二个for越界了。for(int i=1;i<=lb.n;i++)

for(int i = 0; i < lb.n; i++)