...Just run the program and you'll know whats wrong with it...it gives a -1 value...

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

void main()
{
  int array[10],key;
    cout<<"Enter 10 elements:\n";
  for (int c = 0; c < 10; c++)
      cin>>array[c];
      clrscr();
  cout<<"\n List of Elements:";
  for ( c = 0 ; c < 10 ; c++ )
     {
        cout<<" "<<array[c]<<" ";
     }
    cout<< "\nPlease Enter the index of the number to be deleted:";
    cin>>key;
    cout<< "\nNew set of elements:";
  for ( c = 0 ; c < 10 ; c++ )
     {
        if(array[c] == key)
            {
                array[c] = array[c+1];
            }
     }
        cout<<" "<<array[c]<<" ";//Shoud give the list of the new elements where the deleted value wont appear
     }

+Using Turbo c++ 4.5 compiler btw+

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

For starters your header files are wrong.

It should be just <iostream> not sure about conio

void main should be int main.

And your logic looks a bit dangerous.

array[c+1] might go outside the bounds of the array?

no wait...its not wrong...we are just using different Compilers...im using Turbo c++ 4.5..here's an improved version...but it still won't delete the desired element to be deleted...

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

void main()
{
  int array[10],key;
    cout<<"Enter 10 elements:\n";
  for (int c = 0; c < 10; c++)
      cin>>array[c];
      clrscr();
  cout<<"\n List of Elements:";
  for ( c = 0 ; c < 10 ; c++ )
     {
        cout<<" "<<array[c]<<" ";
     }
    cout<< "\nPlease Enter the index of the number to be deleted:";
    cin>>key;

    cout<< "\nNew set of elements:";
        for ( c = 0 ; c < 10 ; c++ )
     {
        if(array[c] == key)
            {
                array[c] = array[c+1];
            }
        cout<<" "<<array[c]<<" ";
     }

     }
Member Avatar for iamthwee

OK but I don't understand your logic.

And you're deleting anything, by the strict sense of the definition.

haha..okay,..thank you for looking at my post though

no wait...its not wrong...we are just using different Compilers.

Yes it is wrong, the C and C++ standards dictate that main() always returns an int, that's the standard way all compilers do it. If your compiler deviates from that then you are likely to encounter problems if you use another compiler.

The way to delete an item is to shuffle all remaining items left to full up the gap and leave the deleted item at the end of the array. In order for that to work properly you have to keep another int counter that indicates the number of valid entries in the array.

Your program asks for the index number to delete, so that if I enter 5 then I want to delete the 5th element of the array, e.g. array[4]. The comparison on line 22 of your program is not needed because it should not be checking the value stored in the array.

What you want is to create a loop that starts out at the number entered (index) to the end of he array.

for(int i = index; i < array_size-1; i++)
   array[i] = array[i+1];

After you get that working you will want to add more error checking, auch as index is beyond the end of the array or a negative value.

Thank you! now the program works...question, can I go to the first code without using goto?? i used goto here so it returns to the first line of code and do the same thing all over again...here's the code:

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

int main()
{
    int array[100], position, c, n;
    char ch;
A:
    cout<<"Enter number of elements:";
    cin>>n;

    cout<<"Enter "<<n<< " elements:\n";

    for ( c = 0 ; c < n ; c++ )
        cin>>array[c];

    cout<<"Enter the location where you wish to delete element:";
    cin>>position;

    if ( position >= n+1 )
        cout<<"Deletion not possible!";
    else
    {
        for ( c = position - 1 ; c < n - 1 ; c++ )
            array[c] = array[c+1];

        cout<<"New set of Elements:\n";

        for( c = 0 ; c < n - 1 ; c++ )
            cout<<" "<<array[c]<<" ";

    }
    cout<< "\nTry another?[y/n]:";
    cin>>ch;
        if(ch=='y'||ch=='Y')
                {
                    clrscr();
                    goto A;
                }
        else
        return 0;
}

You should use a do-while loop instead of goto.

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

int main()
{
    int array[100], position, c, n;
    char ch;
    bool loopAgain = false
    do // do instead of A
    {
        cout<<"Enter number of elements:";
        cin>>n;

        cout<<"Enter "<<n<< " elements:\n";

        for ( c = 0 ; c < n ; c++ )
            cin>>array[c];

        cout<<"Enter the location where you wish to delete element:";
        cin>>position;

        if ( position >= n+1 )
            cout<<"Deletion not possible!";
        else
        {
            for ( c = position - 1 ; c < n - 1 ; c++ )
                array[c] = array[c+1];

            cout<<"New set of Elements:\n";

            for( c = 0 ; c < n - 1 ; c++ )
                cout<<" "<<array[c]<<" ";

        }
        cout<< "\nTry another?[y/n]:";
        cin>>ch;
        if(ch=='y'||ch=='Y')
        {
            loopAgain = ture;  // set the flag to loop again
            clrscr();
        }
        else
        {
            loopAgain = false;  // set the flag to stop the loop
    } while (loopAgian)  // while condition instead of goto

    return 0;
}

oh yea..i forgot all about that...thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.