Hello,
My name is Jenienne and I am currently tearig my hair out over this program which I ruined. It was intended to be a stock taking program with 5 specific different functions. I would like to know if anybody can help me. I have no errors, but the program just refuses to read from the files correctly. This isan urgent enquiry. I have pasted a copy of the program below. If you can help, please e-mail me at [email protected].
Thank you
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Stock
{
//initialise variables
public:
//public attributes & behaviours
Stock();
void setDetails (string,string,float, int);
int stockout(string, string, float, int, int);
int stockin(string, string, float, int, int);
int reorder(string, string, float, int, int);
void displaystock(string, string, float, int);
void sessiontransactions (string, string, int, string);
//void session();
private:
//private attributes and behaviours
string id;
string name;
string transtype;
float price;
int numberInStock;
int noinstock;//number into stock
int noStock;//number out of stock
int reorderlevel;
};
//implementation section
Stock::Stock()
{
string id="";
string name="";
float price=0.0;
double numberInStock=0;
}//end of default constructor
void Stock::setDetails (string id, string name, float pr, int numberIn)
{
//assigns product details;id, name, price
if (id != "x")
//the following applies as long as the user does not enter'x'
{
id=id;
name=name;
price=pr;
numberInStock=numberIn;
}//end if
}//end of setDetails function
int Stock::stockout(string id, string name, float pr, int numberIn, int noStock)
//function used for removing stock
{
string FindId;
//entered by user for comparison against id stored in file
ofstream Stock;
//output file
Stock.open("Stock.dat", ios::app);
//opened in append mode
ofstream Transaction;
//output file to record transaction
Transaction.open("Transaction.dat", ios:ut);
//opened in output mode, so old transactions are over written
cout<<"Please enter product ID (X to stop):";
cin>>FindId;
//get users input
while(FindId!="x")
//while find Id is not equal to x, do this
{
if(FindId==id)
//if findId is the same as one already stored in the file
{
cout<<"Please enter quantity to remove from to stock:";
//Ask the user to enter the number they wish to remove from stock
cin>>noStock;
numberInStock=numberInStock-noStock;
//the new value of number in stock will be numberInStock-noStock
cout<<id<<'\t'<< name <<'\t'<< price<<'\t'<<numberInStock<<endl;
//Display this information from the file
cout<<endl;
Stock<<numberInStock;
//write the adjusted information to the stock file and to the transaction file
Transaction<< id <<'\t'<< name <<'\t'<< numberInStock <<'\t'<< "Stock In" <<endl;
return 0;
}//end if
else if(FindId!=id)
//if the id does not match any stored in the file
{
cout<<"Not an existing product";
cout<<endl;
//return to beginning prompt
cout<<"Please enter product ID (X to stop):";
cin>>id;
}
Stock<<id<<'\t';
//get the stock id from the file for comparison
//close files
Stock.close();
Transaction.close();
} //end while
return 0;
}
//end of stockout function
int Stock::stockin(string id, string FindID, float price, int numberInStock, int noinstock)
//function for removing stock
{
string FindId;
//ID to be entered by user for comparison
ifstream Stock;
Stock.open("Stock.dat", ios::in);
//opens input file for input
ofstream Transaction;
Transaction.open("Transaction.dat", ios:ut);
//opens output file for output
if(Stock.is_open())
//if the stock file is open, do this
{
cout<<"Please enter product ID (X to stop):";
//ask the user for a product ID
cin>>FindId;
//Read the users input
while(!Stock.eof())
//while it is not the end of the stock file
{
Stock>>id;
//get the stock ID from file for comparison
if(FindId==id)
//if the ID entered by the user is equal to the ID stored in the file
{
cout<<id<<'\t'<<name<<'\t'<<price<<'\t'<<numberInS tock<<endl;
//Display ID, name, Price and number in stock
cout<<"Please enter quantity to add to stock:";
//ask the user to enter the quantity to add to stock
cin>>noinstock;
//read users entry
numberInStock=numberInStock+noinstock;
//The new number in stock will be the number in stock + the 'noinstock'
getline(Stock,id,'\t');
}
else if(FindId!=id)
//if the users input is not the same as any within the file
{
//Display the following message
cout<<"Not an existing product. ";
cout<<"Please enter product details:";
//Accept the users input
cin >> id>>name>>price>>numberInStock;
cout << endl;
getline(Stock,id,'\t');
}
}
//close files
Stock.close();
Transaction.close();
}
return 0;
}
int Stock::reorder(string id, string name, float price, int numberIn, int reorderlevel)
//reorder function
{
ifstream Stock;
//open file for input
Stock.open("Stock.dat", ios::in);
if(Stock.is_open())
//if the stock file is open
{
cout<<"Please enter the reorder level:";
//Ask the user to enter the reorder level
cin>>reorderlevel;
getline(Stock,id,'\t');
//get records from file
while(!Stock.eof())
//while it is not the end of file
{
Stock>>name>>price>>numberIn;
//Stock file desc.
if(numberIn <= reorderlevel)
//if the number in stock is less than or equal to the reorder level then
{
//display record
cout<<id<<'\t'<<name<<'\t'<<numberIn<<'\t'<<endl;
cout<<endl;
}
getline(Stock,id,'\t');
}//end while
//close file
Stock.close();
}
else//if the stock file is not open
{
cout<<"File could not be opened."<<endl;
}
return 0;
}
//end of stock reorder
void Stock::displaystock(string id, string name, float price, int numberIn)
//Stock list function
{
ifstream Stock;
Stock.open("Stock.dat", ios::in);
//open file for input to program
Stock>>id>>name>>price>>numberIn;
//stock record desc.
while (!Stock.eof());
//while it is not the end of the stock file
{
//Display record
cout<<"****************************************"<< endl;
cout<<id<<'\t'<<name<<'\t'<<price<<'\t'<<numberIn;
cout<<endl;
Stock>>id>>name>>price>>numberIn;
cout<<"****************************************"<< endl;//dividers for clarity
}
//close stock file
Stock.close();
}
//end of stock list function
void Stock::sessiontransactions (string id, string name, int numberIn, string transtype)
//Transaction list function
{
ifstream Transaction;
Transaction.open ("Transaction.dat", ios::in);
//open transaction file for input to program
Transaction>>id>>name>>numberIn>>transtype;
//transaction record desc.
while (!Transaction.eof());
//while it is not the end of the transaction file
{
cout<<id<<'\t'<<name<<'\t'<<numberIn<<'\t'<<transt ype;
cout<<endl;
Transaction>>id>>name>>numberIn>>transtype;
//get records from file
}
Transaction.close();
}
//end of transaction function