jnestor
jnestor

Reputation: 29

Returning a boolean function

I wish to make to make the contains Element function return either true or false, but it's not returning anything at all. Any help is appreciated heavily!

bool Set::containsElement(int x) const
{
    int element = false;

    for (int i = 0; i < _numItems; i++) {
        if (_items[i] == x)
            element = true;
    }
    return element;
}

// main.cpp

#include <iostream>
#include <cassert>
#include "Set.h"
using namespace std;

int main() {

    Set s1, s2, s3;

    s1.addElement(7);
    s1.addElement(3);
    s1.addElement(5);

    s1.containsElement(3);

    return 0;
}

Upvotes: 0

Views: 1518

Answers (3)

Stan Holodnak
Stan Holodnak

Reputation: 105

First af all element should be declared as 'bool' not 'int'

std::cout<< s1.containsElement(3)<<std::endl;

will show true or false

Upvotes: 0

Ken White
Ken White

Reputation: 125748

Your function returns the value properly. You just fail to use it in your main() function. You need to do something more like

if(s1.containselement(3)))
{
   // Do whatever you want now that you know it's contained
}

Upvotes: 1

Stephen
Stephen

Reputation: 1528

When you call the function in main, you are calling it to no where, so the value is simply lost when called

Upvotes: 2

Related Questions