I need help with my file stream part of my program.

Dec 16, 2013 at 1:58am
my dad is trying to start a business and I am writing this program to simulate our profits. I am having trouble when the program tries to read a file back into memory. when I run through the read function it adds too many items and it doesn't save any of them to the stores vector of items. I can give you any other file you need to see whats going on. I have 12 so there is a lot to go through.

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
86
87
88
89
90
91
92
93
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>


#include "store.h"

using namespace std;

namespace Wall
{
#ifndef STORE_F_STREAM
#define STORE_F_STREAM
   
/****************************************************************
*name:   addItem()                                              *
*input:     The current stores stock object.                    *
*output: none                                                   *
*purpose:adds an item to the stores stock.                      *
****************************************************************/

void addItem(stock &items) //Adds an item to the stores stock list
{
  fstream Item;               //Fstream object to open the file.
  string nameTemp;       //Holds the name of the object.
  string proNumTemp;       //Holds the product number.
  float priceTemp;       //Holds the price of the item.
                           
  cout << "What is the product number?\n"; //Gets info from the user.
  cin.ignore();
  getline(cin, proNumTemp);
  cout << "What is the name of the item you want to add?\n";
  getline(cin, nameTemp);
  cout << "What is the price of that item?\n";
  cin >> priceTemp;

  Item.open("TheBarnItems.txt", ios::app);
           //Opens and saves the info in the file.
  {
    Item << proNumTemp << "& " << nameTemp << "& " << priceTemp << "&\n";    
           //readItems looks for & to stop each string.
  }

  Item.close();
}

/****************************************************************   
*name: readItems()                                              *
*input:    The current stores stock object.                     *
*output: none                                                   *
*purpose: reads the information on all items into memory.       *
****************************************************************/

void readItems(stock &s)
{
  fstream Item;        //Fstream object to open the items file.
  item temp;        //A temporary item object.
  string proNumTemp;    //Holds the name of the object.
  string nameTemp;    //Holds the product number.
  string priceTemp;    //Holds the price of the item.
  int priceInt;        //Converts priceTemp from a string to an int.

  s.itemsClear();    //Clears the items currently held in memory.

  Item.open("TheBarnItems.txt", ios::in); //Open the file.
 
  if (Item)        //If the file opened proceed.
  {
    for (int i = 0; !Item.eof(); i++)    //Loop untill the eof bit is set.
  {

  getline (Item,    proNumTemp, '&'); //Get the product number.
  getline (Item,    nameTemp,   '&'); //Get the product name.
  getline (Item,    priceTemp,  '&'); //Get the price of the product.
  priceInt = atoi (priceTemp.c_str());      //Converts priceTemp to an int.

  temp.setProductNumber(proNumTemp);      //Sets the item objects product
                                            number to proNumTemp.
  temp.setName(nameTemp);          //Sets the item objects name
                                            to nameTemp.
  temp.setPrice(priceInt);          //Sets the item objects price to
                                            priceInt.
  s.itemsPushBack(temp);          //Pushes the temp item onto the back
                                            of the stores item vector.
  }
}
Item.close();
}
#endif
}

(sorry the formatting ended up a little funny.)
Dec 16, 2013 at 5:39pm
I dont know how anyone can help you without a sample of the input file.

I will guess that line 71 suggest that it's reading the file until the end of the file, so that might explain what your seeing.
Dec 18, 2013 at 12:13am
the input file is exactly what it is reading and writing from the file. It is a class with a product number name and price of the item.
...and yes the for loop is supposed to read the entire file. Inside the file every "&" marks a variable for each item and every new line marks another item.

it is using a vector of this class.

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
#include <string>

namespace Wall
{
#ifndef ITEM_CLASS
#define ITEM_CLASS
	class item
	{
	public:
		item(void);
		~item(void);

		std::string		getProductNumber()		{return productNumber;}	//returns the porduct number.
		std::string		getName()				{return name;}			//returns the name of the item.
		float			getPrice()				{return price;}			//returns the price of the item.
		unsigned int	getAmount()					{return amount;}		//returns the number of items in stock.
		float			getMarkUp()				{return markUp;}		//returns the amount the item is marked up.

		void			setProductNumber(std::string cur)	{productNumber = cur;}	//sets the product number to the current value.
		void			setName(std::string cur)		{name = cur;}			//sets the name to the current value.
		void			setPrice(float cur)			{price = cur;}			//sets the price to the current value.
		void			setAmount(unsigned int cur)		{amount = cur;}			//sets the amount to the current value.
		void			amountDec()				{amount--;}				//decrements the item amount.
		void			setMarkUp(float cur)			{markUp = cur;}			//sets the amount to mark up the item.

	private:
		std::string		productNumber;
		std::string		name;
		float			price;
		unsigned int	amount;
		float			markUp;
	};
#endif
}
Topic archived. No new replies allowed.