Thread: Problem

  1. #1
    Unregistered
    Guest

    Problem

    I need help deleting from linked list, with the code i have below it gives me some type of error when i try to delete an account.

  2. #2
    Unregistered
    Guest
    sorry. my code is

    Code:
    if((temprecord->balance) > 0 ) 
    	{
    		printf("%s %s %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,temprecord->balance);
    		tempptr->balance = tempptr->balance + temprecord->balance;
    		printlist(bankrecords);
    		return 0;
    	}
    	if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance))) 
    	{
    		printf("%s %s %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
    		tempptr->balance = tempptr->balance + temprecord->balance;
    		printlist(bankrecords);
    		return 0;
    	}	
    	if((temprecord->balance < tempptr->balance))
    	{
    		printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
    		printf("%s",prev->socialnum);
    		prev->next=tempptr->next;
    		free (tempptr);
    		printlist(bankrecords);
    		return 0;
    	}
    	
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it gives me some type of error when i try to delete an account
    Care to tell us the error? At a glance I can see that your braces don't match up, but that could just be lazy formatting or a bad cut and paste, or a combination thereof. As much as I love debugging "What's the problem" questions, a little direction is useful.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    it gives me a segmentation fault as soon as it reads the first customer who tries to make a withdrawal from his account but does not have enough money. so as soon as it gets to my last if statement i guess.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're probably going too far into the list, step through your code and search for an off by one error where the pointer is NULL yet you still try to do something with it.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    yea i found that when i take away my pointer it prints fine but then its not deleting the record. any idea why my pointer *prev is always NULL?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I imagine that somewhere in your code you want to do this:
    prev = current;
    current = prev->next;

    Yet you aren't, you want to assign the value of current to prev just before you reassign current to the next node. If prev is NULL then you are failing to do that somewhere.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    if the record im trying to delete is the head how do i delete the head. bankrecords is the head by the way so far i have this:

    Code:
    if((temprecord->balance < tempptr->balance))
    	{
    		printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
    		temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
    		if(tempptr == *bankrecords) {
    			prev=tempptr->next;
    			free(tempptr);
    			printlist(bankrecords);
    		}
    		else  {
    			prev->next=tempptr->next;
    			free (tempptr);
    			printlist(bankrecords);
    		}
    		return 0;
    	}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is 'head' a global? If so, it's easy. Otherwise, you have to pass a pointer to a pointer.
    Code:
    if( to_delete == head )
    {
        temp = head;
        head = head->next;
        deleteNode( temp );
    }
    BTW, since you've posted about 40,000 times with this problem, shouldn't you register? Or not. Whatever.

    Quzah.
    Last edited by quzah; 04-16-2002 at 05:55 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Unregistered
    Guest
    no its not global its just in the function im using

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM