Thread: Classes

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Classes

    Hello all, I;ve made a few adjustments to my program, unfortunately I am still missing something. I did not want to complete the program until I found out why it was calculating the balance correctly or why it wasn't taking the phonenumber or balance as one of the inputs . The program should take in all 4 user inputs (after I choose an account number) which are
    first name
    last name
    phone number
    account balance

    Once this information has been entered I should be then able to update the balance and have the program return all of the information entered including the modified balance. Unfortunately something is going wrone. It seems as if it is not recognizing the 4th input and miscalculating whatever I enter as the third input. After I hit enter for each input, after the third it automatically calculates the balance when that third input should technically be for the phone number. Its supposed to then all ow me to enter the balance but it is not. If you have any suggestions please let me know where I might've gone wrong

    Any help would be greatly appreciated
    Thanks in advance

    Silicon Sally


    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<string.h>
    
     struct information {
     
    	int acctnum;
    	char firstname[10];
    	char lastname[12];
    	long int phonenumber;
    	float balance;
        information* left;
        information* right;
      
    	information(int a = 0, char f[10] = "", char l[12] = "", long int p=0, float b = 0.0) {
            acctnum = a;
    		strcpy(firstname, f);
    		strcpy(lastname, l);
    		phonenumber=p;
    		balance = b;
            left=right=NULL;
    	}
    };
    
    
    
     class Customer{
    
      private:
       information *search1,*ptr,*tempsearch;
       
      void enter(information *s, int x, char fn[], char ln[],long int p,float ba){ 
    	if(x==search1->acctnum) 
    		return;
        if(x<s->acctnum){
    	 if(s->left==NULL)
    	 {
          ptr=new information(x,fn,ln,p,ba);
          s->left=ptr; 
    	  return;
    	 }
         else enter(s->left,x,fn,ln,p,ba);
           return;}
         if(x>s->acctnum){
    	 if(s->right==NULL){
    	   ptr=new information(x,fn,ln,p,ba);
    	   s->right=ptr; return;}
           else enter(s->right,x,fn,ln,p,ba);
    		return; }
      } 
    
     
    
    
     information* findInfo(int x){
    		
      information *infosearch, *tmp= search1;
    
    	if (x == tmp->acctnum) 
    		return NULL;
    		
    	while (tmp != NULL) 
    		{
    			if (x == tmp->acctnum) 
    				break;
    			infosearch = tmp;
    			
    			if (x < tmp->acctnum)
    				tmp = tmp->left;
    			else tmp = tmp->right;		
    		}
    		
    		return infosearch;
    		
    	}
    	
    	information* findMinnode(information *s) {
    		
    		while (s->left != NULL){
    			s = s->left;
    		}
    		return s;
    		}
    	
    	information* findMaxnode(information *s) {
    		while (s->right != NULL) {
    			s = s->right;
    		}
    		return s;
    	}
    
    information* find(int x){
    	ptr=search1;
        
    	while(ptr){
    		if(x==ptr->acctnum) 
    			return ptr;
    		if (x>ptr->acctnum)
    			ptr=ptr->right;
    		else ptr=ptr->left;
    	} return NULL;
    }
    
     public:	
    	
    	Customer(){
    		search1=NULL;
    	    ptr=tempsearch=search1;}
        
    	~Customer(){
    	  }
         
    	void insert(int x,char f[],char l[],int p,float b){
    	   
    		if(search1)
    		  enter(search1,x,f,l,p,b);
    	    else
    		  search1=new information(x,f,l,p,b);}
    
    
      
    
    
      bool search(int x){
    	ptr=search1;
        
    	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) {
    		information* parent, *minnode;
    		
    	     if (!search1) 
    			return false;
    		
    		 if (!find(x)) 
    			return false;
    		
    		parent = findInfo(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 = findInfo(minnode->acctnum);
    			if(parent == ptr)
    			{
    				parent = findInfo(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(search1->right);
    			parent = findInfo(minnode->acctnum);			
    			if(parent == search1)
    			{
    				minnode->left = search1->left;
    				search1 = minnode;
    			}
    			else 
    			{
    				search1->acctnum= minnode->acctnum;
    				parent->left = minnode->right;
    				delete minnode;	
    			}
    			return true;
    
    		}
    
      void withdraw(int account, float am){
    	  information *p=find(account);
    	     if((p->balance) >= am){
    			 p->balance -= am;
                 printf("%-6d%-15s%-10s%10.2f\n",p->acctnum,p->firstname,p->lastname,p->phonenumber,p->balance);}
    		    else
    			 printf("You cannot withdraw %.2f. Your balance only contains  %.2f$\n",am, p->balance );
    
    		}
    
      void deposit(int account,float am){
    	  information *p;
    	  p=find(account);
    	    p->balance += am;
    		printf("%-6d%-15s%-10s%10.2f\n",p->acctnum,p->firstname,p->lastname,p->phonenumber,p->balance);}
      
      
    };
    
    	
     struct mainprogram {
      private:
        
    	Customer a;
    
    	void printout() {
    		
            int acc;
    		char fn[10];
    		char ln[12];
    		long int p;
    		float bal;
    			{
    		    
    			{
    				
    		      a.insert(acc,fn,ln,p,bal);	}
    			}
    		menu();
    		
    		return;
    	}
    
    
    	int enterChoice() {
    		int choice;
    
    		 printf("\nPlease enter your choice\n"
    				"1 - Print out results\n"
    				"2 - Update an account (Deposit or Withdrawal)\n"
    				"3 - Create/Add a new account\n"
    				"4 - Delete an existing account\n"
    				"5 - Exit program\n"
    				);
    	    
    		scanf( "%d", &choice );
    		
    		return choice;
    	}
    
    	void menu() {
    		int choice;
    
    		while ( ( choice = enterChoice() ) != 5 ) {
    
    			switch ( choice ) {
    		    case 1:
    				   showinfo();
    				  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];
    		long int p;
    		float b;
    		
    		printf("Please enter the account number that is to be created :\n");
    		scanf("%d",&acct);
            
    		if(!(a.search(acct))&& ( acct>0)){
    	      printf("Enter your firstname, lastname, phone number, and balance: \n");
    		  scanf("%s%s%f", &f,&l,&p, &b);
              a.insert(acct,f,l,p,b);}
    		else
    		  printf("The number you've entered already exists. Please choose another account number to create \n");
    
    	}
    	void updateaccount(){
    		int acct;
    		int i;
            float amount; 
    		
    		printf("Please enter your account number :\n");
    		scanf("%d",&acct);
    		
    		if(a.search(acct)){
    		  printf("Please choose 1 if you are going to make a deposit. \n Please choose 2 if you are making a withdrawal: ");
    		  scanf("%d",&i);
    		  printf("Please enter the transaction amount: ");
    		  scanf("%f",&amount);
    
    		  if(i==2)
    			  a.withdraw(acct,amount);
    			 
    		  else if(i==1)
    			  a.deposit(acct,amount);
    		
    		  else
    			printf("You've entered the wrong choice\n");
    		}
    		else
    			printf("The account number does not exist\n");
    		 
    
    	}
    void showinfo() {
    
    //???????????????????????????????????
    
    
    	}
    	
    
    	void deleteaccount(){
    		int d;
    		cout<<"Please enter the number of the account you would like to delete:";
    		scanf ("%d",&d);
    		if(a.search(d)){
    			a.remove(d);
    			printf("Account# %d has been removed \n",d);}
    		else 
    			printf("This account does not exist \n");
    		
    	}
    
    	public:
         mainprogram() {
    		
    		printout();
    		}
    
    	~mainprogram() {
    		
    	}
    };
    
    
    
    	int main() {
    	mainprogram m;
    	return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Don't cross post ( http://cboard.cprogramming.com/showt...215#post376215 ) it does nothing but make us upset.
    2) Don't post C++ code in the C board.
    3) Read the Forum Guidelines

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM