C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2005, 01:17 PM   #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]);
       }
}
xamlit is offline   Reply With Quote
Old 12-03-2005, 01:34 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
> if (i = d)
Perhaps == instead of =

Didn't your compiler warn you?

Are you using a compiler which is capable of warning you?
Salem is offline   Reply With Quote
Old 12-03-2005, 01:42 PM   #3
Registered User
 
PING's Avatar
 
Join Date: Nov 2004
Location: india
Posts: 493
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];
__________________
Go Petr !!
My Blog
PING is offline   Reply With Quote
Old 12-03-2005, 02:59 PM   #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.
xamlit is offline   Reply With Quote
Old 12-03-2005, 04:50 PM   #5
Registered User
 
PING's Avatar
 
Join Date: Nov 2004
Location: india
Posts: 493
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.
__________________
Go Petr !!
My Blog
PING is offline   Reply With Quote
Old 12-03-2005, 04:53 PM   #6
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22