i want to delete a element from the array
i use this logic but it is not working

#include<iostream.h>
#include<conio.h>
main()
{
int arr[5];     //10 element int array
int key;        // key to edit
int length=5;   //length of array
clrscr();
for(int i=0; i<length ; i++)
{
cin>>arr[i];
}
cout<<"Enter item no u want to del";
cin>>key;
for(i=0; i<length ; i++)
    {
	if(arr[i] == key)
	{
		arr[i] = arr[i+1];
	}
   }
for(int i=0; i<length ; i++)
{
cout<<arr[i];
}

getch();

}

Edited 6 Years Ago by Nick Evan: added code tags

You need to shift all elements past the one to be deleted. Currently you only shift the first, which leaves you with something not quite what you wanted. Compare with this, which deletes the first occurrence of the selected value:

for(int i=0; i<length; i++)
{
    if(arr[i] == key)
    {
        while (i < length) {
            arr[i] = arr[i+1];
            ++i;
        }

        break;
    }
}

Since, Narue has pointed how to delete the first occurrence of the selected value. I will show how to delete every occurrence of the selected value.

int i=0, j=0;

    while(i<length) {
        if (i > j) {
            arr[j] = arr[i];
        }

        if (arr[i] == key) {
            i++;
        } else {
            i++;
            j++;
        }
    }

Edited 6 Years Ago by invisal: n/a

actually i want this item to delete plz help me out

#include<iostream.h>
#include<conio.h>

class employee
{
private:
 char name[30];
 unsigned int pay;
public:
 void getdata()
  {
   cout<<"\n\tEnter The Name: ";     cin>>name;
   cout<<"\n\tEnter pay dues:";       cin>>pay;
  }
void showdata()
  {
    cout<<"\t\t"<<name<<"\t\t"<<pay<<endl;
  }
};


main()
{
	    int del;
	    int n=5;
	    int x;
		  employee e[100];

		  clrscr();

		  for(x=0;x<n;x++)
		  {
			cout<<"\n\nEnter employe record No: "<<endl;
			  e[x].getdata();
		  }


	   cout<<"Enter the record no you want to del";
	   cin>>del;

			for(x=0; x<n; x++)
				{
				   if(x == del)
				   {
				    while (x < n)
					  {
					   e[x] = e[x+1];
					   ++x;
					  }
				   }
				 }


	for(x=0; x<n; x++)
	 e[x].showdata();


getch();
}

When you delete an element, the number of items in the array decreases by one. You need your length to reflect that:

for(x=0; x<n; x++)
{
    if(x == del)
    {
        while (x < n)
        {
            e[x] = e[x+1];
            ++x;
        }

        --n;
        break;
    }
}

And for the record:

  1. main returns int. C++ doesn't support implicit int. I don't care what your compiler allows. Do it correctly so I don't have to modify your code before it'll compile on anything but Turbo C and Visual C++ 6.
  2. Don't just copy boilerplate code. If you don't understand it, you end up with ridiculous patterns like this one:
    #include <conio.h>
    
    int main()
    {
      // Don't try to compile me outside of Turbo C
      // I don't care about any programs that ran before me
      // But it's cool because I can't decide whether I own the window or not[1]
      clrscr();
    
      /* Your code here */
    
      // Yay for zero portability!
      // I'm probably annoying and unnecessary
      // I can't decide whether I own the window or not[1]
      getch();
    }

[1] For those wondering, it's only necessary to pause for input when the window is closed upon termination. This typically only happens when the window is created for the program's process (ie. double clicky or running from some IDEs), in which case there's no previous output to clear with clrscr. Thus having both clrscr at the beginning and getch at the end is nonsensical; it suggests complete lack of understanding of one's code.

>can anyone post the full edited code here...plzzzzzz
How helpless can you be!? I gave you the working loop. All you need to do is cut and paste, Einstein. Geez.

Here is full code

#include<iostream.h>
#include<conio.h>

class employee
{
private:
 char name[30];
 unsigned int pay;
public:
 void getdata()
  {
   cout<<"\n\tEnter The Name: ";     cin>>name;
   cout<<"\n\tEnter pay dues:";       cin>>pay;
  }
void showdata()
  {

    cout<<"\n\n\n\t\t"<<name<<"\t\t"<<pay<<endl;
  }
};


main()
{
	    int del;
	    int n=5;
	    int x;
		  employee e[100];

		  clrscr();

		  for(x=1;x<n;x++)
		  {
			cout<<"\n\nEnter employe record No: "<<x<<endl;
			  e[x].getdata();
		  }


	   cout<<"Enter the record no you want to del";
	   cin>>del;

			for(x=1; x<n; x++)
				{
				   if(x == del)
				   {
				    while (x < n)
					  {
					   e[x] = e[x+1];
					   ++x;
					  }
				   }
				 }

	       n--;

 ///////////////////////////////////////////
// showing data

clrscr();
  cout<<"\t\t********Showing data*********";
	for(x=1; x<n; x++)
	e[x].showdata();


getch();
}
Comments
You're unbelievable..

>Here is full code
I'm impressed. It was a simple hack job and you still managed to make the code worse. You've got talent.

This question has already been answered. Start a new discussion instead.