mardi 29 décembre 2015

Getting unexpected behavior of if condition

template <class T>
class Queue {
  private:

    array<T,10> elems;
    int frt=-1,bck=-1;    
};

When i run the following code of snippet

template<class T>
void Queue<T>::Enque(T const& item){
    cout<<"Size of array:"<<elems.size()<<"\n";
    if(bck<elems.size())
    {

        if(bck==-1)frt=0;
        ++bck;
    elems.at(bck)=item;
    cout<<"Element Inserted\n";
    }

    else
        cout<<"Queue is overflow\n";

}

I get this output

enter image description here

Here it is showing that the size is 10 and the value of bck is -1 but it is going to the else statement.

But if I replace the if condition with if(bck<10) the code is working fine.

enter image description here

Can anyone help me in identifying this behavior.

Aucun commentaire:

Enregistrer un commentaire