5 Replies - 864 Views - Last Post: 16 September 2011 - 11:37 AM Rate Topic: -----

#1 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

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.

#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error reading from file; class, array

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,672
  • Joined: 23-August 08

Re: Error reading from file; class, array

Posted 15 September 2011 - 03:54 PM

Copy and paste the error exactly as it appears.
Was This Post Helpful? 0
  • +
  • -

#3 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

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;
Was This Post Helpful? 0
  • +
  • -

#4 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

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
Was This Post Helpful? 0
  • +
  • -

#5 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

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.

This post has been edited by Mina-kun: 16 September 2011 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,672
  • Joined: 23-August 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1