Hey guys.

These functions are not working together correctly because of the true/false statements. Can someone please show me how I can fix the empty() function to work simultaneously with my other function provided? :S

My getSmallest() function will not return the value due to the empty() function..

``````bool set::empty() const
{
for (int i = 0; i <= positions.size(); i++)
{
if (i = -1)
{
return true;
}
return false;
}
}

int set::getSmallest() const
{
for (int i = 0; i < DEFAULTSIZE; i++)
{
if (setlist.at(i) == 1)
{
positions.push_back(i);
}
}

if ( !positions.empty() )
{
return *( min_element( positions.begin(), positions.end() ) );
}
return -1;
}``````

Thanks :)

Edited by scantraXx-: n/a

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by dkalita

``if (i = -1)``

the condition is always true
try using

``if (i == -1)``

``if (i = -1)``

the condition is always true
try using

``if (i == -1)``

also see

``````for (int i = 0; i <= positions.size(); i++)
{
if (i = -1) //considering it as (i==-1)
{
return true;
}
return false;
}``````

u have initialized i to 0 and did i++ so when will it become ==-1.
It will always return false in the first iteration itself.

Why not :

``bool isEmpty() { return ( position.size == 0 ); }``

Why not :

``````bool isEmpty() { return ( position.size == 0 ); }
//make it position.size()``````

yes thats the perfect solution

Edited by dkalita: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.