0

Below is the basic structure code for my super market billing and stock editing program class:

class Admin
{
public:
     admin();
private:
     int Pid;
     char name[20];
     double quant;
     double price;
     double disc;
     double net_price;

     friend istream &read(istream&, Admin&);
     friend ostream &show(ostream&, const Admin);
};

istream &read(istream&, Admin&);
ostream &show(ostream&, const Admin);

definition of friend functions:

istream &read(istream &is, Admin &commodity)
{
    double dis;

    fflush(stdin);
    is>>commodity.Pid;

    is.getline(commodity.name,30);

    is>>commodity.quant
      >>commodity.price
      >>commodity.disc;

    dis=grs_pr*(commodity.disc/100);

    commodity.net_price=grs_pr-dis;

    return is;
}

ostream &show(ostream &os, const Admin thing)
{
    os << thing.name <<"   " << thing.quant <<"   "
       << thing.price <<"   " << thing.disc <<"   "
       << thing.net_price << endl;

    return os;
}

Main function:

int main()
{
    admin item;

    while(read(cin,item)
    {
        ofstream file;
        file.open("Stock.dat",ios::binary | ios::app);
        file.write(reinterpret_cast<const char*>(&item),sizeof(Admin));
    }
    ifstream readFile("Stock.dat",ios::in|ios::binary);

    while(!readFile.eof())
    {
        readFile.read(reinterpret_cast<char*> (&item),sizeof(Admin));
        Admin readedItem;
        read(readFile,readedItem);    /*i have used read and show function to watch the values that are read by readFile but every time show function output some five exponential values like 5.23689e-301*/
        show(cout,readedItem);
    }

    return 0;
}

Please tell me what is wrong in above code and what should i do to correct it, my motive is to read from the file created and to alter or edit the data of the item that is to be selected by mentioning the product ID by the user, please help me how to attain this functionality.

4
  • What specific problem you are facing? Commented Mar 23, 2013 at 9:53
  • @n.m.my problem is that i am not able to read the data (objects) from the file Commented Mar 23, 2013 at 9:58
  • "I am not able to do this or that" is not specific. "I am getting this error (paste the text of the error)" is. "I expect the program to print FOO but it prints BAR" is. Commented Mar 23, 2013 at 10:06
  • 1
    One problem is that the code you've shown won't compile. For example, in main(), you have while(read(cin,item) {. This is simply wrong, missing a parenthesis. Your code should be an SSCCE (Short, Self-Contained, Correct Example). Amongst other things, that means it should compile! Commented Mar 23, 2013 at 10:47

2 Answers 2

1

you shouldn't implement a naiive serialization yourself. Use a library.

See i.e. answers 1, 2+comments, 3, 4

Also, don't put logic of data manipulation (i.e. commodity.net_price=grs_pr-dis;) in your data serialization

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming I'm understanding right then your read and show loop should be

    for (;;)
    {
        readFile.read(reinterpret_cast<char*> (&item),sizeof(Admin));
        if (readFile.eof())
            break;
        show(cout,item);
    }

For some reason you were reading twice, first into a variable called item and then into another variable called readItem. You only need to read once. Also while (!readFile.eof()) is wrong because you must test for end of file after you read not before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.