Thread: Problem about file

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

    Problem about file

    This part has a problem for saving in switch case. After deposit, the file only save the first account and the other account will be deleted.
    How to solve this problem?

    How to set an error message if account not found or end of file?



    Code:
    case 'D':
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				int acNo;
    				double bal1,bal2;
    				system("cls");
    				r.open("checking.txt");
    				cout<<"\nDeposit into checking account";
    				cout<<"\nEnter your checking account number: ";
    				cin>>acNo;
    				for(int i=0;!r.eof()+1;i++)
    				{
    					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;
    					}
    				}
    				/*if(r.eof())
    				{
    					cout<<"\nSorry, no such account.";
    					cout<<"\n\nPress ENTER to continue...";
    					while(!cin.get()){};
    					//while(!cin.get()){};
    				}*/
    				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;
    Last edited by nasa; 03-14-2004 at 06:25 AM.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> How to set an error message if account not found or end of file?
    This is how I usually do it
    Code:
    found = false;
    while (r) {
        // go through the file
       if(acNo==che[i].getacctNum()) {
          found = true;
          // do stuff
          break;
       }
    }
    
    if (found == false) {
        cout << "not found\n";
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM