C++ 子结构

标签 c++ c structure

嘿,我想获得一些有关子结构的帮助。 我按照以下说明进行锻炼: 有 1 个库

该图书馆有 10 位作者(书籍作者)

每个图书作者最多可以拥有 15 本书。

我必须使用两个函数: - 阅读所有信息 - 将书籍从 A 到 Z 排序

我现在写了结构,但由于子结构,我迷失了如何阅读它们

代码在这里:http://pastebin.com/gMaZXR89

这就是我已经走了多远,我陷入了那些“得到”。 我将不胜感激任何帮助,谢谢

最佳答案

我想我解决了你的问题......

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

struct Book
{
    char title[30];//unneceserly use Author author[30] because you refer at the book by its author
    float price;
    int year_of_publication;
};

struct Author
{
    char name[30],subname[30];
    int birth_date;
    Book x[14];//a max of 15 books/author
};

struct Library
{
    /*there are 10 book writers*/Author y[9];//because you defined struct Author now you can use it without struct.
};

void readBooks(struct Library lib/*there is a single library*/, const int n=10)//there are 10 book writers
{
    int i,j,how_many;
    for(i=0;i<n;i++)
    {
        cout<<"Input "<<i+1<<". authors name"<<endl;
        gets(lib.y[i].name);//you call the array y not the type Author...
        cout<<"Input "<<i+1<<". authors subname"<<endl;
        gets(lib.y[i].subname);
        cout<<"How many books author"<<i+1<<" write?";
        cin>>how_many;

        for(j=0;j<how_many;++j)
           {
              cout<<"Input "<<i+1<<". authors "<<j+1<<" book title "<<endl;
              gets(lib.y[i].x[j].title);
              cout<<"Input "<<i+1<<". authors "<<j+1<<" book price "<<endl;
              cin>>lib.y[i].x[j].price;
              cout<<"Input "<<i+1<<". authors "<<j+1<<" book year of publication "<<endl;
              cin>>lib.y[i].x[j].year_of_publication;
           }
    }
}
int main()
{
    Library l;//can`t write struct in front because it is defined up...
    readBooks(l);
    return 0;
}

关于C++ 子结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23853716/

相关文章:

c++ - 公共(public)静态类成员函数和命名空间中声明的全局函数有什么区别?

c - 在C编程中达到EOF时如何退出stdin

c++ - strcpy() 导致段错误?

c - 结构类型数组。在结构内部有指针

c++ - 在 C++ 中从文件中读取 double 的最快方法

C++函数无法使用模板为基类的子类派生模板

c - 静态链接的可执行文件的启动代码发出这么多系统调用?

c - If结构语句

c++ - 未声明的结构名称和交换函数在没有通过引用传递的情况下进行交换

C++ 返回但未存储的值会发生什么?