Thread: Deleting element from an array problem

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Deleting element from an array problem

    Hey guys!,

    I am trying to remove a user specified element from the display of an array. I know that this doesn't take it out of the array, but I figure that if I get this it would be the same process copying it to a temp array and then back.

    Code:
    printf("Please enter number to delete");
       scanf("%d",&d);
       
       printf("\n\n\nModified list:");
       
       for (i=0;i<x;i++) 
       {   
           if (i = d) 
           {
                 i = i+1;
           }
           else 
           {
           printf("%s",words[i]);
           }
    }

  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
    > if (i = d)
    Perhaps == instead of =

    Didn't your compiler warn you?

    Are you using a compiler which is capable of warning you?

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    You need not start from 0, if the user inputs the number into the variable d, just start out with that number itself.
    Code:
    for(i=d;i<MAX-1;i++)
    array[i]=array[i+1];
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Quote Originally Posted by PING
    You need not start from 0, if the user inputs the number into the variable d, just start out with that number itself.
    Code:
    for(i=d;i<MAX-1;i++)
    array[i]=array[i+1];

    Hmm.. but doesn't that just start from the element they entered? I want to be able to display all the elements as if it were removed. such as :

    1.box
    2.shoe
    3.red
    4.crayon

    user selects to delete red from the display, new display should look like this:

    1.box.
    2.shoe
    3.crayon

    I hope I'm making this clear enough.

    I appreciate any help you give me.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    just replace this
    Code:
    array[i]=array[i+1];
    by this
    Code:
    strcpy(array[i],array[i+1]);
    This is valid considering that you are using a 2d char array to store the different strings.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And you should set array[0] to something, too.

    Code:
    if (i = d)
    ->
    Code:
    if (i == d)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Problem deleting dynamic array
    By pliang in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2005, 04:07 PM