Prolem about file saving.

This is a discussion on Prolem about file saving. within the C++ Programming forums, part of the General Programming Boards category; If the file has more than one account and one of the account is doing deposit. After deposit, the program ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    Prolem about file saving.

    If the file has more than one account and one of the account is doing deposit. After deposit, the program only save the changes for the account that doing deposit and other account will be deleted.

    How to solve the problem?

    Code:
    case 'D':
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				int acNo,found=0,i;
    				double bal1,bal2;
    				system("cls");
    				r.open("checking.txt");
    				cout<<"\nDeposit into checking account";
    				cout<<"\nEnter your checking account number: ";
    				cin>>acNo;
                                                                    i=-1;  
    				while(!r.eof())				{
                                                                                    i=i+1;
    					r.read((char*)(&che[i]),sizeof(che[i]));
    					if(acNo==che[i].getacctNum())
    					{	
    						bal1=che[i].getBal();
    						cout<<"\n\nFound in "<<i<<endl;
    						w.open("checking.txt");
    						che[i].deposit();
    						w.write((char*)(&che[i]),sizeof(che[i]));
    						w.close();
    						bal2=che[i].getBal();
    						tra[n].setTran(acNo,"Deposit","Success",bal1,bal2);
    						w.open("transaction.txt",ios::app,ios::ate);
    						w.write((char*)(&tra[n]),sizeof(tra[n]));
    						w.close();
    						break;
    					}
    				}
    				r.close();				
    				break;
    			}
    			if(acctType=='S')
    			{
    				sav[n].deposit();
    				break;
    			}
    			else
    			{
    				cout<<"Invalid type";
    				cout<<"\n\nPress ENTER to continue...";
    				while(!cin.get()){};
    				while(!cin.get()){};
    			}
    			break;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >After deposit, the program only save the changes for the account that doing deposit and other account will be deleted.
    What other account? Are we talking about a temporary scratch account object? If not, why aren't you treating accounts separately? Assuming the use of a single file for all accounts, you really want to place everything into memory and process it. Then when you're done, write back to the file:
    Code:
    map<long, account> checking = load_accounts ( in );
    long acct_num = get_account ( cin );
    if ( checking.find ( acct_num ) != checking.end() ) {
      switch ( get_option ( cin ) ) {
      ...
      case 'D':
        checking[acct_num].deposit ( amount );
        log_trans ( "Deposit", "Checking", acct_num, checking[acct_num] );
        break;
      ...
      }
    }
    ...
    if ( changes_made )
      write_accounts ( checking, out );
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Somehow, this poster thinks that by spamming the board with enough copies of the code, someone will eventually spend some time and figure out what is wrong.

    It's just a snip of what they "think" the problem is from this massive post
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21