Thread: Stock Taking program

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    Thumbs up Stock Taking program

    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I guess you're new, so I'll go easy on you

    Please read this thread which tells you how and why to use code tags.

    >>This isan urgent enquiry.
    Anything marked as urgent tends to get ignored, a last minute panic is your problem, not ours.

    >>If you can help, please e-mail me at
    This is a learning community, questions and answers are posted on these forums, personal email replies are not part of the game. If you want to be notified via email when someone responds to your thread, you can click the "Receive updates to this thread" link.

    Now to answer your question (maybe):

    >>while (!Stock.eof());
    >>while (!Transaction.eof());
    This does nothing, the semi-colon is not required, and is causing problems with your code. You also should read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    if you could post a test file, that would be great.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    one more tip: turn off smilies...

    for example:
    Transaction.open("Transaction.dat", ios:ut);

    should be:
    Transaction.open("Transaction.dat", ios::out);
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM