Inputting data from txt file into array object
Sep 14, 2011 at 4:44pm UTC
I am trying to enter data into an array object. The array size is 50, because the final data file will contain no more than 50 items. My code compiles, but when trying to display the information, I get long, incorrect integers such as -85449350. Any help is appreciated!
Below is my class header file, cpp code, and the data file.
inventory_class.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <string>
using namespace std;
class Inventory
{
public :
Inventory(); //class constructor
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;
}
//***************************************************************
inventory.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <string>
#include <fstream>
#include "inventory_class.h"
using namespace std;
//***********************************************************
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();
}
//***********************************************************
int main ()
{
Inventory inv_array[50];
int num_items = 0;
loadArray(inv_array, num_items);
for (int i = 0; i < num_items; i++)
{
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;
}
return 0;
}
inv_data.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14
13
Keyboard
300
21.49
13
Keyboard
300
21.49
13
Keyboard
300
21.49
Topic archived. No new replies allowed.