【发布时间】:2012-12-03 16:18:48
【问题描述】:
struct collection
{
string title, author, isbn;
float price;
bool availability;
int borrow;
};
void read1(member a[]);
void read2(collection b[]);
int main()
{
member a[20];
collection b[100];
read1(a);
read2(b);
}
这是我正在尝试运行的功能。它第一次运行良好,但第二次围绕 getline 没有阅读书名并跳过它。然后它会在稍后的第二个 getline 中读取它。
void read2(collection b[])
{
ifstream database;
string n1;
cout << "Enter second input file name: ";
getline(cin, n1);
database.open(n1.c_str());
if(database.fail())
{
"Bad file. \n" ;
}
else
{
for(int j=0;!database.eof();j++)
{
getline(database, b[j].title);
cout << b[j].title<<endl;
getline(database,b[j].author);
cout<<b[j].author<<endl;
database>>b[j].isbn;
cout<<b[j].isbn<<endl;
database>>b[j].price;
cout<<b[j].price<<endl;
database>>b[j].availability;
cout<<b[j].availability<<endl;
database>>b[j].borrow;
cout<<b[j].borrow;
}
database.close();
}
}
【问题讨论】:
-
cout 以后不会出现了。我只是将其放入以检查数据是否正确放入结构数组中。
-
你正在阅读的文件是什么样的?
-
不要同时使用
getline和>>。许多类似的问题,但简短的版本是getline删除了'\n'而>>没有。 -
您的代码还有一个问题:stackoverflow.com/questions/5605125/…