嘿,我想得到一些子结构方面的帮助。 我按照以下说明锻炼身体: 那里有1个图书馆
在该图书馆中有10位作者(书籍作者)
每位图书作家最多可以有15本书。
我必须使用2个功能: - 阅读所有信息 - 从A到Z排序书籍
我写了结构,现在我因为子结构而迷失了如何读取它们
代码在这里:http://pastebin.com/gMaZXR89
那我走了多远,我被困在那些"得到"。 我将不胜感激任何帮助,谢谢
答案 0 :(得分:2)
我想我解决了你的问题......
#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;
}
答案 1 :(得分:0)
你的问题在于
gets(tab[i].Author[y].name);
gets(tab[i].Author[y].subname);
Author不是您在Library结构中定义的Author类型的结构数组的名称,它的名称是y。
所以它应该是这样的:
gets(tab[i].y[j].name);
gets(tab[i].y[j].subname);
祝你好运。