我正在尝试显示文本文档中的所有数据。该程序显示它已成功读取14条记录,这是正确的。但是,当我显示全部时,它会显示()。我已经标出了我认为我的问题发生的地方。问题发生在我的整个程序中(所有空的返回),但如果我得到showAll的帮助,我想我可以将它应用到其他地方。注意:我删除了部分代码。
struct Book
{
string title;
string author;
};
int loadData (Book books[], string);
void showAll (Book books[], int count);
int showBooksByAuthor (int string);
int showBooksByTitle (int string);
int loadData(Book books, string inputFileName)
{
ifstream inputFile;
int i = 0;
inputFile.open(inputFileName.c_str());
if ( ! inputFile.is_open())
{
cout << "Unable to open input file." << endl;
return -1;
}
while( ! inputFile.eof())
{
getline(inputFile, books.title);
getline(inputFile, books.author);
i++;
}
return i;
}
void showAll(Book books[], int count) //This may need fixing, the only way I
// was able to get this to work is by removing
// Book books[] from void showAll. So the
// function would read void showAll(int
// count). It is a requirement that I use
// int showAll(Book books[], int count)
{
for (int i = 0; i < count; i++)
{
cout << books[i].title << " (" << books[i].author << ")" << endl;
}
}
int main()
{
int count;
int i = 0;
string inputFileName;
string title;
string name;
string word;
char reply;
cout << "Location of library file: ";
cin >> inputFileName;
count = loadData(books[i], inputFileName); //This may need fixing
loadData(books[i], inputFileName); //This may need fixing
cout << count << " Records loaded successfully." << endl << endl;
cout << "Enter Q to (Q)uit, A to search (A)uthor, T to search (T)itle, "
<< endl << "S to (S)how all: ";
cin >> reply;
switch(reply)
{
case 'S':
cout << endl;
showAll(books, count); //This may need fixing
break;
case 's':
cout << endl;
showAll(books, count); //This may need fixing
break;
}
return 0;
}
我希望我的结果能够显示showAll的标题(作者)。我的任何功能都没有任何回报。我认为我的问题是正确使用struct Book books []。我尝试改变一些变量,但最终得到了错误。
实际结果是()。没有错误。我对结构广告数组很少有经验,或者对于这个问题一般编码。我希望我已经解释得很好。感谢您的任何意见。