【发布时间】:2009-11-09 21:25:12
【问题描述】:
当我尝试在 Visual C++ 2008 Express 上编译以下代码时出现系统错误。我要做的是用从文件中读取的数据初始化对象数组。我认为 while 循环内部有问题,因为当我在没有 while 循环的情况下手动初始化这些对象时,它似乎可以工作。这是代码和文本文件:
#include <iostream>
#include <string>
#include "Book.h"
using namespace std;
int main()
{
const int arraySize = 3;
int indexOfArray = 0;
Book bookList[arraySize];
double tempPrice;//temporary stores price
string tempStr;//temporary stores author, title
fstream fileIn( "books.txt" );
while ( !fileIn.eof( ))
{
getline(fileIn,tempStr);
bookList[indexOfArray].setAuthor(tempStr);
getline(fileIn,tempStr);
bookList[indexOfArray].setTitle(tempStr);
fileIn >> tempPrice;
bookList[indexOfArray].setPrice(tempPrice);
if ( indexOfArray < arraySize ) //shifting array index while not exceeding array size
indexOfArray++;
}
fileIn.close();
return 0;
}
和文本文件:
Author1
Book1
23.99
Author2
Book2
10.99
Autho3
Book3
14.56
【问题讨论】:
-
“系统错误”的具体内容是什么?
标签: c++ arrays file initialization