Thread: Class Bank(i have a question)

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    Class Bank(i have a question)

    hi

    i create a class bank that creates updates and deletes accounts
    using class BST(binary search tree) and creates a structure node which holds the client data there's no compilation error in the program but there's somthing wrong with storing the information in a text file i've written a function that's print's the content of the tree on a file called "saveinfo" using preorder i want this information to be saved after i excute the program so when i run it again i read from this file and save what it contains in a tree i did this in the constructor of the clas Bank but it doesn't work properly
    please can you tell me what's wrong ?

    Code:
    #include<stdio.h>
    #include<string.h>
    
     struct node {
     
    	int acctnum;
    	char lastName[15];
    	char firstName[10];
    	float balance;
        node* left;
        node* right;
      
    	node(int c = 0, char f[10] = "", char l[15] = "", float b = 0.0) {
            acctnum = c;
    		strcpy(lastName, l);
    		strcpy(firstName, f);
    		balance = b;
            left=right=NULL;
    	}
    };
    
    
    
     class BST{
    
      private:
       node *root,*ptr,*iterator;
       
      void _insert(node *s, int x, char fn[], char ln[],float ba){ 
    	if(x==root->acctnum) 
    		return;
        if(x<s->acctnum){
    	 if(s->left==NULL)
    	 {
          ptr=new node(x,fn,ln,ba);
          s->left=ptr; 
    	  return;
    	 }
         else _insert(s->left,x,fn,ln,ba);
           return;}
         if(x>s->acctnum){
    	 if(s->right==NULL){
    	   ptr=new node(x,fn,ln,ba);
    	   s->right=ptr; return;}
           else _insert(s->right,x,fn,ln,ba);
    		return; }
      } 
    
     void _inorder(node *s,FILE *x){
    
    	 if(s){ 
    	 _inorder(s->left,x);
    	 fprintf(x,"%-6d%-16s%-11s%10.2f\n",s->acctnum,s->lastName,s->firstName,s->balance);
    	 _inorder(s->right,x);
    }
     }
    
     void _preorder(node *s,FILE *x){
    
    	 if(s){
          fprintf(x,"%-6d%-16s%-11s%10.2f\n",s->acctnum,s->lastName,s->firstName,s->balance);
          _preorder(s->left,x);
          _preorder(s->right,x);}
    	
    }
    
     void _postorder(node *s){
       
    	 if(s){
      	  _postorder(s->left);
    	  _postorder(s->right);
    	  delete s;}
    }
    
    
    
     node* findParent(int x){
    		
      node *parent, *tmp= root;
    
    	if (x == tmp->acctnum) 
    		return NULL;
    		
    	while (tmp != NULL) 
    		{
    			if (x == tmp->acctnum) 
    				break;
    			parent = tmp;
    			
    			if (x < tmp->acctnum)
    				tmp = tmp->left;
    			else tmp = tmp->right;		
    		}
    		
    		return parent;
    		
    	}
    	
    	node* findMinnode(node *s) {
    		
    		while (s->left != NULL){
    			s = s->left;
    		}
    		return s;
    		}
    	
    	node* findMaxnode(node *s) {
    		while (s->right != NULL) {
    			s = s->right;
    		}
    		return s;
    	}
    
    node* find(int x){
    	ptr=root;
        
    	while(ptr){
    		if(x==ptr->acctnum) 
    			return ptr;
    		if (x>ptr->acctnum)
    			ptr=ptr->right;
    		else ptr=ptr->left;
    	} return NULL;
    }
    
     public:	
    	
    	BST(){
    		root=NULL;
    	    ptr=iterator=root;}
        
    	~BST(){
    	  postorder();}
         
    	void insert(int x,char f[],char l[],float b){
    	   
    		if(root)
    		  _insert(root,x,f,l,b);
    	    else
    		  root=new node(x,f,l,b);}
    
    
       void inorder(FILE *s){
         _inorder(root,s);
       } 
    
    
       void preorder(){
    	//_preorder(root);
       }
    
    
      void postorder(){
    	_postorder(root);
      }
    
    
      bool search(int x){
    	ptr=root;
        
    	while(ptr){
    		if(x==ptr->acctnum) 
    			return true;
    		if (x>ptr->acctnum)
    			ptr=ptr->right;
    		else ptr=ptr->left;
    	} return false;
    }
    
    
      bool remove(int x) {
    		node* parent, *minnode;
    		
    	     if (!root) 
    			return false;
    		
    		 if (!find(x)) 
    			return false;
    		
    		parent = findParent(x);
            
    		
    		
    		if (parent) 
    		{
    			
    			if ((ptr->left == NULL) && (ptr->right == NULL))
    			{
    				if (parent->left == ptr) 
    					parent->left = NULL;
    				else 
    					parent->right = NULL;
    				delete ptr;
    				return true;
    			}
    			
    			if (ptr->left == NULL) 
    			{
    				if (parent->left == ptr)
                        parent->left = ptr->right;
    				else 
    					parent->right = ptr->right;
    				delete ptr;
    				return true;
    			}
    			
    			if (ptr->right == NULL) 
    			{
    				if (parent->left == ptr)
    					parent->left = ptr->left;
    				else 
    					parent->right = ptr->left;
    				delete ptr;
    				return true;
    			}
    		
    		
    			minnode = findMinnode(ptr->right);
    			parent = findParent(minnode->acctnum);
    			if(parent == ptr)
    			{
    				parent = findParent(ptr->acctnum);
    				minnode->left = ptr->left;
    				if( ptr == parent->left)
    					parent->left =minnode;
    				else
    					parent->right =minnode;
    				delete ptr;
    			}
    			else 
    			{
    				ptr->acctnum = minnode->acctnum;
    				parent->left = minnode->right;
    				delete minnode;	
    			}
    			return true;
    			
    			
    		}
    		else 
    			minnode = findMinnode(root->right);
    			parent = findParent(minnode->acctnum);			
    			if(parent == root)
    			{
    				minnode->left = root->left;
    				root = minnode;
    			}
    			else 
    			{
    				root->acctnum= minnode->acctnum;
    				parent->left = minnode->right;
    				delete minnode;	
    			}
    			return true;
    
    		}
    
      void withdraw(int account, float am){
    	  node *p=find(account);
    	     if((p->balance) >= am){
    			 p->balance -= am;
                 printf("%-6d%-16s%-11s%10.2f\n",p->acctnum,p->lastName,p->firstName,p->balance);}
    		    else
    			 printf("you cannot withdraw %.2f$ your balance contains only %.2f$\n",am, p->balance );
    
    		}
    
      void deposit(int account,float am){
    	  node *p;
    	  p=find(account);
    	    p->balance += am;
    		printf("%-6d%-16s%-11s%10.2f\n",p->acctnum,p->lastName,p->firstName,p->balance);}
      
      
    };
    
    	
     struct Bank {
      private:
        FILE *fp;
    	BST a;
    
    	void readfile() {
    		
            int acc;
    		char fn[10];
    		char ln[15];
    		float bal;
    			if ( ( fp = fopen( "bank.txt", "r" ) ) != NULL ){
    		    
    			while(!feof(fp)){
    				
    			  fscanf(fp,"%d",&acc);
    		      fscanf(fp,"%s",ln);
    		      fscanf(fp,"%s",fn);
    		      fscanf(fp,"%f",&bal);
    		      a.insert(acc,fn,ln,bal);	}
    			}
    		menu();
    		
    		return;
    	}
    
    
    	int enterChoice() {
    		int choice;
    
    		 printf("\nEnter your choice\n"
    				"1 - save information in the text file \n"
    				"    \"bank.txt\" for printing\n"
    				"2 - update an account\n"
    				"3 - add a new account\n"
    				"4 - delete an account\n"
    				"5 - end program\n"
    				"(Note:Befor Ending save your information in a file by choosing first selection)\nYour choice: " );
    	    
    		scanf( "%d", &choice );
    		
    		return choice;
    	}
    
    	void menu() {
    		int choice;
    
    		while ( ( choice = enterChoice() ) != 5 ) {
    
    			switch ( choice ) {
    			case 1:
    				   saveinfo();
    				  break;
    			case 2:
    			      updateaccount();
    				   break;
    			case 3:
    				   newaccount();
    					break;
    			case 4:
    				   deleteaccount();
    				   break;
    			default: printf("\nPlease select from 1-5\n\n");
    			
    			}
    
    		}
    	}
    
     
    	void newaccount(){
    		int acct;
    		char f[10];
    		char l[15];
    		float b;
    		
    		printf("Enter number of account u want to create from :\n");
    		scanf("%d",&acct);
            
    		if(!(a.search(acct))&& ( acct>0)){
    	      printf("Enter your firstname, lastname, balance: ");
    		  scanf("%s%s%f",&f,&l,&b);
              a.insert(acct,f,l,b);}
    		else
    		  printf("the number u entered  is in use or it doesn't exist \n");
    
    	}
    	void updateaccount(){
    		int acct;
    		int i;
            float amount; 
    		
    		printf("enter ur account number :\n");
    		scanf("%d",&acct);
    		
    		if(a.search(acct)){
    		  printf("enter 1 if you want to deposit or 2 for withdrawaing: ");
    		  scanf("%d",&i);
    		  printf("Enter the amount of money: ");
    		  scanf("%f",&amount);
    
    		  if(i==2)
    			  a.withdraw(acct,amount);
    			 
    		  else if(i==1)
    			  a.deposit(acct,amount);
    		
    		  else
    			printf("you didn't enter the right # for depositing or withdrawing\n");
    		}
    		else
    			printf("The account number doesn't exist\n");
    		 
    
    	}
    
    
    	void saveinfo() {FILE *w;	
    		if ( ( w = fopen( "bank.txt", "w" ) ) == NULL )
    			printf( "File could not be opened.\n" );
    		else {
    			fprintf( w, "%-6s%-16s%-11s%10s\n","Acct", "Last Name", "First Name","Balance" );
                a.inorder(w);}
    	}
    	void deleteaccount(){
    		int j;
    		printf("enter number of account u want to delete: ");
    		scanf ("%d",&j);
    		if(a.search(j)){
    			a.remove(j);
    			printf("account %d was removed \n",j);}
    		else 
    			printf("This account doesn't exist\n");
    		
    	}
    
    	public:
         Bank() {
    		
    		readfile();
    		}
    
    	~Bank() {
    		fclose(fp);
    	}
    };
    
    
    
    	int main() {
    	Bank a;
    	return 0;
    }
    Last edited by kuwait; 05-30-2003 at 04:45 AM.
    please set free our POWs

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    thanx

    Thanx for telling me that

    ok after i do this i want to save the accounts on a file i did the function saveinfo and this its code



    Code:
    void saveinfo() {
    		
    		FILE *w;	
    		
    	    if ( ( w = fopen( "bank.txt", "w" ) ) == NULL )
    			printf( "File could not be opened.\n" );
    		else {
    			fprintf( w, "%-6s%-16s%-11s%10s\n","Acct", "Last Name", "First Name","Balance" );
                a.inorder(w);}
    	}

    functions public inorder and private _inorder in class BST:


    Code:
    void inorder(FILE *s){
         _inorder(root,s);
       }


    Code:
    void _inorder(node *s,FILE *x){
    
    	 if(s){ 
    	  _inorder(s->left,x);
    	  fprintf(x,"%-6d%-16s%-11s%10.2f\n",s->acctnum,s->lastName,s->firstName,s->balance);
    	  _inorder(s->right,x);}
     }

    and the function which saves the content of the file the in a tree
    after running the program again is readfile and this its code


    Code:
     void readfile() {
    		
            int acc;
    		char fn[10];
    		char ln[15];
    		float bal;
    			if ( ( fp = fopen( "bank.txt", "r" ) ) != NULL ){
    		    fscanf(fp,"\n");
    			while(!feof(fp)){
    				
    			  fscanf(fp,"%d",&acc);
    		      fscanf(fp,"%s",ln);
    		      fscanf(fp,"%s",fn);
    		      fscanf(fp,"%f",&bal);
    		      a.insert(acc,fn,ln,bal);	}
    			}
    		menu();
    		
    	}

    my question is how can i read the file and save it's content
    in a tree?
    i did it by the way shown above but it doesn't
    work correct.
    Last edited by kuwait; 05-30-2003 at 04:48 AM.
    please set free our POWs

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    this is the text file which was saved by function saveinfo

    Code:
    Acct  Last Name       First Name    Balance
    -858993460ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ-107374176.00
    1     b               a               10.00
    5     mh              sa              20.00
    8     yo              ah             300.00
    why does the second line of this file appears like this
    please can you tell me how can i do it right?
    and can you tell me please when i want to read the file
    and store the accounts in a tree how can i skip the first line?
    Last edited by kuwait; 05-30-2003 at 05:27 AM.
    please set free our POWs

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    Thanx for all i knew how to solve it
    Last edited by kuwait; 05-30-2003 at 08:08 AM.
    please set free our POWs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM