0

So I am error checking the codes I learned from given data structure book.I found out the insert function is causing error.Here is my code so far:

#include<iostream>
using namespace std;
class list
{
    int Myarray[10];
    int Mysize;

    public:
        list(void)
        {Mysize=0;}

    bool Empty()
    {
        return Mysize==0;
    }

    void Display()
    {
        for(int i=0;i<Mysize;i++)
        {
          cout<<Myarray[i]<<"";
        }
    }

    void Insert(int item,int pos)
    {
        if(Mysize==10)
        {
            cout<<"Full";

        }
        if(pos<0 ||pos >Mysize)
        {
            cout<<"Error";

        }

        for(int i=Mysize;i>pos;i--)
        {
            Myarray[i]=Myarray[i-1];
        }

            Myarray[pos]=item;
            Mysize++;



    }


    void Erase(int pos)
    {
        if(Mysize==0)
        {
            cout<<"Empty";
            return;
        }

        if(pos<0 || pos>= Mysize)
        {
            cerr<<"Error";
            return;
        }

        for(int i=pos;i<Mysize;i++)
        {
            Myarray[i]=Myarray[i+1];
        }
            Mysize--;

    }
};
    int main()
    {
        list X;

for (int i = 0; i < 9; i++)
{
cout<< "Inserting "<<i<<" at position "<<i/2<<endl;
X.Insert(i, i/2);


}
cout<<endl; 
X.Display();
cout <<"\nTry to insert at position -1" <<endl;
X.Insert(0, -1) ;
cout<<endl; 
X.Display();



cout << "\nTry to insert at position 10"<< endl;
X.Insert(0, 10);
cout<<endl; 
X.Display();
}

The result is:

Inserting 0 at position 0
Inserting 1 at position 0
Inserting 2 at position 1
Inserting 3 at position 1
Inserting 4 at position 2
Inserting 5 at position 2
Inserting 6 at position 3
Inserting 7 at position 3
Inserting 8 at position 4

135786420
Try to insert at position -1
Error
0135786420
Try to insert at position 10
Full
0

What I don't understand is that since I have the condition:

if(pos<0 ||pos >Mysize)
{cout<<"Error";}

Why is that when inserting 0 into -1 position which supposed to be invalid is also inserted as display in the result?Furthermore when a value is inserted into 10th position it resets the whole array and becomes 0?Isn't it the condition in the Insert function should have terminate both these conditions?

2
  • I suggest you do some rubber duck debugging of your Insert function. Or use an actual debugger to step through it statement by statement. The latter should make it very obvious what the problem is. Commented Jan 1, 2020 at 11:51
  • Change the type of pos to std::uint32_t, then you dont need to care about pos<0 Commented Jan 1, 2020 at 11:51

1 Answer 1

3

It's because you always proceed to the 'for' loop afterwards. You need to either use: 'if', 'else if' and then put the 'for' in 'else' or simply return after printing 'full' or 'error'. For example:

void Insert(int item,int pos)
{
    if(Mysize==10)
    {
        cout<<"Full";
    }
    else if(pos<0 ||pos >Mysize)
    {
        cout<<"Error";
    }
    else {
        for(int i=Mysize;i>pos;i--)
        {
            Myarray[i]=Myarray[i-1];
        }
        Myarray[pos]=item;
        Mysize++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.