Thread: Problem about file.

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

    Problem about file.

    Can some body help me in this program.
    The problem already write in the command of source code below.
    I also upload the source file.

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<ctype.h>
    #include<windows.h>
    #include<iomanip.h>
    #include<fstream.h>
    #include<time.h>
    
    class saving;
    
    class checking
    {
    	int acctNum;
    	double balance;
    public:
    	int getacctNum(){return acctNum;}
    	double getBal(){return balance;}
    	void openAcct();
    	void deposit();
    	void withdraw(saving &);
    	void showBalance();	
    };
    
    class saving
    {
    	int acctNum;
    	double balance;
    public:
    	int getacctNum(){return acctNum;}
    	double getBal(){return balance;}
    	void openAcct();
    	void deposit();
    	void withdraw();
    	void showBalance();
    	friend void checking::withdraw(saving &);
    };
    
    class transaction
    {
    	int acctNum;
    	char trans[15];
    	char status[15];
    	double balance1;
    	double balance2;
    public:
    	int getacctNum(){return acctNum;}
    	void setTran(int acNo, char t[], char s[], double bal1, double bal2)
    	{
    		acctNum=acNo;
    		strcpy(trans,t);
    		strcpy(status,s);
    		balance1=bal1;
    		balance2=bal2;
    	}
    	void displayTran()
    	{
    		cout<<"\nAccount Number: "<<acctNum
    			<<"\nTransaction: "<<trans
    			<<"\nStatus: "<<status
    			<<"\nPrevious Balance: "<<balance1
    			<<"\nCurrent Balance: "<<balance2
    			<<endl;
    	}
    };
    
    class menu
    {
    public:
    	void mainmenu()
    	{
    		cout<<"\nWelcome to UniBank";
    		cout<<"\n\nTransaction types available";
    		cout<<"\nO - Open account";
    		cout<<"\nD - Deposit";
    		cout<<"\nW - Withdraw";
    		cout<<"\nB - Balance";
    		cout<<"\nT - Transaction";
    		cout<<"\nX - Exit";
    	}
    	char typeMenu(void)
    	{
    		char acctType;
    		system("cls");
    		cout<<"\n\nAccount types available";
    		cout<<"\nC - Checking";
    		cout<<"\nS - Saving";
    		cout<<"\n\nEnter account type: ";
    		cin>>acctType;
    		acctType=toupper(acctType);
    		return acctType;	
    	}
    };
    
    void checking::openAcct()
    {
    	system("cls");
    	cout<<"\nOpen checking account...";
    	cout<<"\nEnter account number: ";
    	cin>>acctNum;
    	cout<<"\nEnter starting amount: $";
    	cin>>balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void checking::deposit()
    {
    	double amount;
    	cout<<"\nYour current balance is $"<<balance;
    	cout<<"\n\nEnter amount to deposit: $";
    	cin>>amount;
    	balance=balance+amount;
    	cout<<"\nYour new balance is $"<<balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void checking::withdraw(saving &s)
    {
    	int acNo;
    	double amount,sf;
    	char ans;
    	system("cls");
    	cout<<"\nWithdraw from checking account...";
    	do
    	{
    		cout<<"\nEnter your cheking account number: ";
    		cin>>acNo;
    		if(acNo!=acctNum)
    		{
    			cout<<"\nInvalid account; try again";
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    			return;
    		}
    	}while(acNo!=acctNum);
    	if(acNo==acctNum)
    	{
    		cout<<"\nEnter amount to withdraw: $";
    		cin>>amount;
    		if(amount>balance)
    		{
    			sf=amount-balance;
    			cout<<"\nSorry, insufficient balance";
    			cout<<"\nAre you want to transfer the shortfall from the saving account? [y/n]: ";
    			cin>>ans;
    			ans=toupper(ans);
    			if(ans=='Y')
    			{
    				cout<<"\nTransfer shortfall from saving to checking account...";
    				do
    				{
    					cout<<"\nEnter saving account number: ";
    					cin>>acNo;
    					if(acNo!=s.acctNum)
    					{
    						cout<<"\nInvalid account; try again";
    						cout<<"\n\nPress ENTER to continue...";
    						while(!cin.get()){};
    						while(!cin.get()){};
    					}
    				}while(acNo!=s.acctNum);
    				if(sf>s.balance)
    				{
    					cout<<"\nInsufficient funds";
    					cout<<"\n\nPress ENTER to continue...";
    					while(!cin.get()){};
    					while(!cin.get()){};
    					return;
    				}
    				else
    				{
    					s.balance=s.balance-sf;
    					balance=balance+sf;
    					balance=balance-amount;
    					cout<<"\nPlease take your cash";		
    					cout<<"\nYour new checking balance is $"<<balance;
    					cout<<"\nYour new saving balance is $"<<s.balance;
    					cout<<"\n\nPress ENTER to continue...";
    					while(!cin.get()){};
    					while(!cin.get()){};		
    				}
    			}
    			else
    			{
    				cout<<"\n\nPress ENTER to continue...";
    				while(!cin.get()){};
    				while(!cin.get()){};
    				return;
    			}
    		}
    		else
    		{
    			balance=balance-amount;
    			cout<<"\nPlease take your cash";
    			cout<<"\nYour new checking balance is $"<<balance;
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    		}
    	}
    }
    
    void checking::showBalance()
    {
    	cout<<"\nYour balance is $"<<balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void saving::openAcct()
    {
    	system("cls");
    	cout<<"\nOpen saving account...";
    	cout<<"\nEnter account number: ";
    	cin>>acctNum;
    	cout<<"\nEnter starting amount: $";
    	cin>>balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void saving::deposit()
    {
    	double amount;
    	cout<<"\nYour current balance is $"<<balance;
    	cout<<"\n\nEnter amount to deposit: $";
    	cin>>amount;
    	balance=balance+amount;
    	cout<<"\nYour new balance is $"<<balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void saving::withdraw()
    {
    	int acNo;
    	double amount;
    	system("cls");
    	cout<<"\nWithdraw from saving account...";
    	do
    	{
    		cout<<"\nEnter your saving account number: ";
    		cin>>acNo;
    		if(acNo!=acctNum)
    		{
    			cout<<"\nInvalid account; try again";
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    			return;
    		}
    	}while(acNo!=acctNum);
    	if(acNo==acctNum)
    	{
    		cout<<"\nEnter amount to withdraw: $";
    		cin>>amount;
    		if(amount>balance)
    		{
    			cout<<"\nSorry, insufficient balance";
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    			return;
    		}
    		else
    		{
    			cout<<"\nPlease take your cash";
    			balance=balance-amount;
    			cout<<"\nYour new balance is $"<<balance;
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    		}
    	}
    }
    
    void saving::showBalance()
    {
    	cout<<"\nYour balance is $"<<balance;
    	cout<<"\n\nPress ENTER to continue...";
    	while(!cin.get()){};
    	while(!cin.get()){};
    }
    
    void main()
    {
    	const int n=100;
    	checking che[n];
    	saving sav[n];
    	transaction tra[n];
    	menu men;
    	char tranType,acctType;	
    	ifstream r;
    	ofstream w;
    	do
    	{
    		system("cls");
    		men.mainmenu();
    		cout<<"\nEnter choice: ";
    		cin>>tranType;
    		tranType=toupper(tranType);
    		switch(tranType)
    		{
    		case 'O':
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				int acNo;
    				r.open("checking.txt");
    				cout<<"\nEnter account number: ";
    				cin>>acNo;
    				for(int i=0;!r.eof();i++)
    				{
    					r.read((char*)(&che[i]),sizeof(che[i]));
    					if(acNo==che[i].getacctNum())
    					{
    						cout<<"\nAccount already exist.";
    						cout<<"\n\nPress ENTER to continue...";
    						while(!cin.get()){};
    						while(!cin.get()){};
    						break;
    					}	
    					else
    					{
    						if(r.eof())
    						{
    							w.open("checking.txt",ios::app,ios::ate);
    							che[i].openAcct();
    							w.write((char*)(&che[i]),sizeof(che[i]));
    							w.close();
    						}
    					}
    				}
    				r.close();
    				break;
    			}
    			if(acctType=='S')
    			{
    				int acNo;
    				r.open("saving.txt");
    				cout<<"\nEnter account number: ";
    				cin>>acNo;
    				for(int i=0;!r.eof();i++)
    				{
    					r.read((char*)(&sav[i]),sizeof(sav[i]));
    					if(acNo==sav[i].getacctNum())
    					{
    						cout<<"\nAccount already exist.";
    						cout<<"\n\nPress ENTER to continue...";
    						while(!cin.get()){};
    						while(!cin.get()){};
    						break;
    					}	
    					else
    					{
    						if(r.eof())
    						{
    							w.open("saving.txt",ios::app,ios::ate);
    							sav[i].openAcct();
    							w.write((char*)(&sav[i]),sizeof(sav[i]));
    							w.close();
    						}
    					}
    				}
    				r.close();
    				break;
    			}
    			else
    			{
    				cout<<"Invalid type";
    				cout<<"\n\nPress ENTER to continue...";
    				while(!cin.get()){};
    				while(!cin.get()){};
    			}
    			break;
    		case 'D':
    //this part has a problem for saving
    //after deposit, the file only save the first account and the other account will be deleted
    //how to solve this problem?
    			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;
    		case 'W':
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				che[n].withdraw(sav[n]);
    				break;
    			}
    			if(acctType=='S')
    			{
    				sav[n].withdraw();
    				break;
    			}
    			else
    			{
    				cout<<"Invalid type";
    				cout<<"\n\nPress ENTER to continue...";
    				while(!cin.get()){};
    				while(!cin.get()){};
    			}
    			break;
    		case 'B':
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				int acNo;
    				system("cls");
    				r.open("checking.txt");
    				cout<<"\nShow checking account balance";
    				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())
    					{
    						cout<<"\n\nFound in "<<i<<endl;
    						che[i].showBalance();
    						break;
    					}	
    				}
    //how to set an error message if account not found or end of file?
    				/*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')
    			{
    				int acNo;
    				system("cls");
    				r.open("saving.txt");
    				cout<<"\nShow saving account balance";
    				cout<<"\nEnter your saving account number: ";
    				cin>>acNo;
    				for(int i=0;!r.eof()+1;i++)
    				{
    					r.read((char*)(&sav[i]),sizeof(sav[i]));
    					if(acNo==sav[i].getacctNum())
    					{
    						cout<<"\n\nFound in "<<i<<endl;
    						sav[i].showBalance();
    						break;
    					}	
    				}
    				/*if(r.eof())
    				{
    					cout<<"\nSorry, no such account.";
    					cout<<"\n\nPress ENTER to continue...";
    					while(!cin.get()){};
    					while(!cin.get()){};
    				}*/
    				r.close();
    				break;
    			}
    			else
    			{
    				cout<<"Invalid type";
    				cout<<"\n\nPress ENTER to continue...";
    				while(!cin.get()){};
    				while(!cin.get()){};
    			}
    			break;
    		case 'T':
    //what happen for this part went program execute?
    			acctType=men.typeMenu();
    			if(acctType=='C')
    			{
    				int acNo;
    				system("cls");
    				r.open("transaction.txt");
    				cout<<"\nTransaction done";
    				cout<<"\nEnter your checking account number: ";
    				cin>>acNo;
    				for(int i=0;!r.eof()+1;i++)
    				{
    					r.read((char*)(&tra[i]),sizeof(tra[i]));
    					if(acNo==tra[i].getacctNum())
    					{			
    						tra[i].displayTran();
    					}
    				}
    				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()){};
    			}
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    			break;
    		case 'X':
    			break;
    		default:
    			cout<<"\nInvalid choice, try again";
    			cout<<"\n\nPress ENTER to continue...";
    			while(!cin.get()){};
    			while(!cin.get()){};
    		}
    		cout<<endl;
    	}while(tranType!='X');		
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Produce a simple test program which demonstrates the same problem.

    1. The simple exercise of actually doing this may tell you where you messed up. If you're still stuck, then you can post a nice simple program to the board.

    2. Nobody here wants to wade through hundreds of lines of code.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    40
    Not only does nobody want to, but nobody will. There's a limit to how far someone will go in the name of a charitable contribution. I wouldn't want to look through my own code for errors if it was that big, let alone someone else's code.
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    7
    You all can just test the program.
    After you finish open account and deposit, you can test the selection for 'T' and it will and windows error prompt out.
    Base on that error, can you tell me what happen of this.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I need the header file backward_warning.h

    Without it it won't compile..and that may be your problem as well. We need the full source.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by nasa
    You all can just test the program.
    After you finish open account and deposit, you can test the selection for 'T' and it will and windows error prompt out.
    Base on that error, can you tell me what happen of this.
    Ok Fine.
    Error E2449 xx.cpp 94: Size of 'checking' is unknown or zero
    Error E2141 xx.cpp 94: Declaration syntax error
    *** 2 errors in Compile ***
    Tested. Fix those errors. After you finish, heed Salem's and ShadowMetis' suggestions. You should not be telling us to test your program, you should be doing what was suggested and asking what else you can do to get closer to your own solution.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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