mauricioSanchez
mauricioSanchez

Reputation: 366

int function that returns an index from a vector of bools

I have the following function:

int FamiliesController::returnFamilySelected(){

for ( int i = 0; i < isFamilyCoosenController.size(); i++){

    if ( isFamilyChosenController[i] == true){

            return i;
        }   
    }
}

This function returns the index of a set of booleans, that when one of the is true it returns its index in the vector, very straight forward. The problem that I have now is that when all the booleans are false. The function returns a weird number -1029384. And the moment if I compare them like this:

int FamiliesController::returnFamilySelected(){

for ( int i = 0; i < isFamilyChosenController.size(); i++){

    if ( isFamilyChosenController[i] == true){

            return i;
        }else if ( is FamilyIsChosenController[i] == false){
            return - 1;
     }   
    }
}

It will always return -1. Because it doesn't matter if only one element is true, once it passes that bool in the vector, it will return false right away, since the next one is false. I need to have the index of the one that has become true always be returned.

Is there anyway to tell it for example(pseudo code):

for ( int i = 0; i < isFamilyChosenController.size(); i++){// we check in our vector of booleans

   if ( One boolean == true ){

       return the index of that boolean;

   }else if ( all the booleans == false ){

     return -1; // a number that is out of the container size but I can manipualte


}

Let's note here that I'm not using c++ 11.

Thanks!

Upvotes: 0

Views: 814

Answers (3)

fmilani
fmilani

Reputation: 531

Well, your code will only work as expected if the first element of the vector is true. Instead, you could try this:

int FamiliesController::returnFamilySelected(){

int index = -1;

    for ( int i = 0; i < isFamilyChosenController.size(); i++){

        if ( isFamilyChosenController[i] == true){

            index = i;
            break;
        }
    }
    return index;
}

Upvotes: 1

pm100
pm100

Reputation: 50110

your problem is that you dont return anything if you dont find a match

A good compiler will at least warn you about this

int FamiliesController::returnFamilySelected(){

for ( int i = 0; i < isFamilyCoosenController.size(); i++){

    if ( isFamilyChosenController[i] == true){

            return i;
        }   
    }
}
// now what?????

You need to return a sentinel value to say 'not found'

int FamiliesController::returnFamilySelected(){

for ( int i = 0; i < isFamilyCoosenController.size(); i++){

    if ( isFamilyChosenController[i] == true){

            return i;
        }   
    }
}
return -1; // not found

or you could throw

Upvotes: 4

Jon
Jon

Reputation: 437336

Just put the return -1 after the loop. Either one of the values satisfies your condition and you return its index, otherwise the loop ends and you return -1.

That said, the idiomatic way to do this in C++ is using std::find.

Upvotes: 5

Related Questions