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.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include "store.h"
usingnamespace 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
}
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.
#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.
unsignedint 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(unsignedint 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;
unsignedint amount;
float markUp;
};
#endif
}