Thread: Classes and output

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

    Classes and output

    Hello all, I've been having a bit of a problem with my program. I am a novice at C but have managed to learn it through reading multiple books and practicing. Unfortunately I am stuck on this one. Ive written a program that will take info entered (first name, last name, phone, balance) for a user. This info will then be taken so that I could adjust their end balance. ONce I make this adjustment, it will return all of the users info including the new balance. It sounds easy, but I;ve been racking my brain trying to figure this out. When I run my program I am only able to enter 3 out of 4 inputs and when i try and update the balance, I get an odd number on every occasion. I was wondering if anyone had any ideas or if they could point me in the right direction. Also I wanted to print all of the users information that was entered. Am I on the right track or should I skip that part until I read a little more (Deitel)
    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%-16s%-11s%10.2f\n",p->acctnum,p->firstname,p->lastname,p->phonenumber,p->balance);}
    		    else
    			 printf("you cannot withdraw %.2f$ your balance contains only %.2f$\n",am, p->balance );
    
    		}
    
      void deposit(int account,float am){
    	  information *p;
    	  p=find(account);
    	    p->balance += am;
    		printf("%-6d%-16s%-11s%10.2f\n",p->acctnum,p->firstname,p->lastname,p->phonenumber,p->balance);}
      
      
    };
    
    	
     struct mainprogram {
      private:
        FILE *fp;
    	Customer a;
    
    	void readfile() {
    		
            int acc;
    		char fn[10];
    		char ln[12];
    		long int p;
    		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,"%s",&p);
    		      fscanf(fp,"%f",&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() {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" );
               }
    	}
    	
    
    	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() {
    		
    		readfile();
    		}
    
    	~mainprogram() {
    		fclose(fp);
    	}
    };
    
    
    
    	int main() {
    	mainprogram m;
    	return 0;
    }
    Last edited by silicon; 06-18-2004 at 05:54 AM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Please use code tags... you can edit your post and add [code] to the top of the code and [/code] to the bottom. Use the preview changes button to make sure it worked.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm not touching that until you use code tags. Check my signature for the link.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Hello, I have placed the program within the code tags

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Classes using phone numbers
    By correlcj in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2002, 10:17 PM