It's driving me nuts.
I need to read from a file of data in the following format:
(int showing total number of objects in file)
(string)Name1
(int)ID1
(double)Price1
(int)Stock1
(string)Name2
(int)ID2
(double)Price2
(int)Stock2
... and so on.
I'm reading each set as an object called Item. Here's my reading-in code.
int main() {
int count = 0;
Item itemArray[50];
ifstream fp("myFile.txt", ios::in);
if(!fp.is_open()) {
cerr << "Error while opening file." << endl;
return 0;
}
while (!fp.eof() || count < 10) {
//THE PROBLEM IS HERE, OUTPUT STATEMENT HERE PRINTS OUT, BUT NOTHING AFTER THIS.
for (int i = 0; i < 10; i++) {
fp >> itemArray[i];
count++;
}
}
fp.close();
}
I've also overloaded the >> operator to read the data into the object array directly:
class Item {
friend istream& operator >> (istream&, Item&);
...
};
istream& operator >>(istream& fp, Item& i) {
fp >> i.name;
fp >> i.id;
fp >> i.price;
fp >> i.stock;
return fp;
}
The while thing gets a 'fatal error' message (Windowspeak for 'coredump' I guess?) Oh, and another thing. The coredump DOES NOT HAPPEN when my file contains nothing.
But as soon as I put something into my file, DUMP!
Please help!
-Nandini