#include <iostream> #include <string> #include <fstream> using namespace std; //*********************************************************** class Inventory { public: Inventory(); void setItemNumber(int i); void setDescription(string d); void setQuantity(int q); void setCost(double c); void setTotalCost(); int getItemNumber(); string getDescription(); int getQuantity(); double getCost(); double getTotalCost(); private: int item_number; string description; int quantity; double cost; double total_cost; }; //*************************************************************** Inventory::Inventory() { item_number, quantity, cost, total_cost = 0; description = ""; } //*************************************************************** void Inventory::setItemNumber(int i) { item_number = i; } //*************************************************************** void Inventory::setDescription(string d) { description = d; } //*************************************************************** void Inventory::setQuantity(int q) { quantity = q; } //*************************************************************** void Inventory::setCost(double c) { cost = c; } //*************************************************************** void Inventory::setTotalCost() { total_cost = cost * quantity; } //*************************************************************** int Inventory::getItemNumber() { return item_number; } //*************************************************************** string Inventory::getDescription() { return description; } //*************************************************************** int Inventory::getQuantity() { return quantity; } //*************************************************************** double Inventory::getCost() { return cost; } //*************************************************************** double Inventory::getTotalCost() { return total_cost; } //*************************************************************** void loadArray (Inventory inv_array[], int &num_items) { int itemNum, qty; double cost; string description; fstream inFile; inFile.open("inv_data.txt", ios::in); if (inFile) { while (!inFile.eof()) { inFile >> itemNum; inv_array[num_items].setItemNumber(itemNum); inFile.ignore(); getline(inFile, description); inv_array[num_items].setDescription(description); inFile >> qty; inv_array[num_items].setQuantity(qty); inFile >> cost; inv_array[num_items].setCost(cost); num_items++; } } else cout << "Error reading from file" << endl; inFile.close(); } void showArray(Inventory inv_array[], int num_items) { cout << inv_array[num_items].getItemNumber() << endl; cout << inv_array[num_items].getDescription() << endl; cout << inv_array[num_items].getQuantity() << endl; cout << inv_array[num_items].getCost() << endl; num_items++ } //*********************************************************** int main () { Inventory inv_array[50]; int num_items = 0; loadArray(inv_array, num_items); showArray(inv_array, num_items); return 0; }
Error reading from file; class, array
Page 1 of 15 Replies - 1384 Views - Last Post: 16 September 2011 - 11:37 AM
#1
Error reading from file; class, array
Posted 15 September 2011 - 02:35 PM
Getting a read from file error, can't see what I'm doing wrong. I've got a text file attached with inventory items. Any suggestions are welcome.
Replies To: Error reading from file; class, array
#2
Re: Error reading from file; class, array
Posted 15 September 2011 - 03:54 PM
Copy and paste the error exactly as it appears.
#3
Re: Error reading from file; class, array
Posted 15 September 2011 - 04:01 PM
It builds and runs, just going to my else statement:
else
cout << "Error reading from file" << endl;
else
cout << "Error reading from file" << endl;
#4
Re: Error reading from file; class, array
Posted 16 September 2011 - 10:58 AM
Can anyone help? Is it a problem of the class reading wrong in to the array?
Thanks
Thanks
#5
Re: Error reading from file; class, array
Posted 16 September 2011 - 11:19 AM
Try running the executable itself, rather than debugging through your IDE. Put the text file in the same directory as the executable - it seems most likely that the application simply can't find the file.
To fix this in your IDE, you'll have to specify where the debug directory is (the default and how to do this varies based on IDE). In Visual Studio, I recommend adding inv_data.txt to the project, opening the properties window after selecting the file in the solution explorer, and marking the file as Content.
To fix this in your IDE, you'll have to specify where the debug directory is (the default and how to do this varies based on IDE). In Visual Studio, I recommend adding inv_data.txt to the project, opening the properties window after selecting the file in the solution explorer, and marking the file as Content.
This post has been edited by Mina-kun: 16 September 2011 - 11:20 AM
#6
Re: Error reading from file; class, array
Posted 16 September 2011 - 11:37 AM
The way you've written the code, the file must be in the same directory as the executable (or the working directory). If it's not, it won't open it.
Page 1 of 1