-3

I am writing a code for Banking system and got stuck and am unable to point out what's wrong. Can anyone help me?

This functions was to modify the balance from accounts stored in a binary file 'Accounts.bat'. but the loop is never ending.

    void transaction :: process()
    {  int bflag=0;
       if(type==0||type==1)
       {    account b;
            fstream file("Accounts.bof",ios::binary);
            file.seekg(0,ios::beg);
            while(!file.eof())
            {   file.read((char*)&b, sizeof(b));
                if(file.eof())
                break;
                if(b.get_acno()==payee.ac)
                {   if(b.get_balance()>=amount)
                    {   b.minus_money(amount);
                        bflag=1;
                        file.seekp(-1*(sizeof(b)),ios::cur);
                        file.write((char*)&b, sizeof(b));
                        b.out();               //to output account's details
                        getch();
                        break;
                        cout<<"Money deducted";
                        getch();
                    }
                }
                file.write((char*)&b, sizeof(b));
            }
            if(bflag==0)
            cout<<"\n\n\t\tTransaction unsuccessful!!!";
            cout<<"\n\t\tCause : Insufficient Balance.";
            file.close();
        }
        if(type==0||type==2)
        {   account b;
            fstream file("Accounts.bof",ios::binary);
            while(!file.eof())
            {   file.read((char*)&b, sizeof(b));
                if(!file.eof())
                break;
                if(b.get_acno()==recieptent.ac)
                {   b.add_money(amount);
                    cout<<"money added";
                    file.seekp(-1*(sizeof(b)),ios::cur);
                    file.write((char*)&b, sizeof(b));
                    break;
                }
                file.write((char*)&b, sizeof(b));
            }
            file.close();
        }
        if(bflag)
        cout<<"\n\n\t\tTransaction successful.";
        getch();
    }
  • 5
    Learn how to use a debugger. – dandan78 Dec 14 '17 at 14:25
  • 1
    You should put together a short but complete example and provide your input so we can see which code paths are being used. You say your loop never ends, but which one? Consider putting your magic numbers in an enum so they have names. You should also read this: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong While your usage isn't completely incorrect it's still a good idea to make sure you're testing your reads and writes for success or failure. – Retired Ninja Dec 14 '17 at 14:29
  • It's because you're reading the file, while writing to the file after using `seekp()`. You're saying "Read until the end of the file, where if I get this certain data, start back at the beginning of the file and write to it" – Treyten Carey Dec 14 '17 at 14:31
  • 2
    Welcome to Stack Overflow! This is a problem that can be easily solved, or at least drastically narrowed down by **debugging**. Please take the time to learn how to use a debugger. It's immensely helpful and will you save you many hours of just staring at your code. I cannot stress enough how important is to able to debug your own code. Learn it sooner rather than later! – bolov Dec 14 '17 at 14:32

1 Answers1

1

Your file is ever growing, since you are writing to the file at the same time you are reading it

You may want to make sure your file's put position matches the actual spot where you read your record by making explicit calls to tellg() and seekp(). This should clear the problem. This is what good coding practice would recommend, anyway.

while(!file.eof())
{   
    auto pos = file.tellg();          // note down your spot
    file.read((char*)&b, sizeof(b));
    //...

    file.seekp(pos);                  // overwrite the same exact spot in file.
    file.write((char*)&b, sizeof(b));
}
Michaël Roy
  • 5,382
  • 1
  • 12
  • 17