Booleans and Functions

I am writing a code where I have to find out the spot a letter is in. I am getting an error with assigning a function using a boolean operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool is_member(const vector<char> & list, char character)
{
    for(int i=0; i < list.size(); i++)
    {
        if(character==list[i])
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
    return 0;
}
int find_index(const vector<char> & list, char character)
{
    for(int i=0; i < list.size();i++)
    {
        if(is_member(list[i]==character))
        {
            return(i);
        }
    }
    
    return (-1);
}


My constant vector list is { 'a', 'e', 'i', 'o', 'u', 'y'}. My error comes in on line 20. I am not calling the boolean correctly. If I type in the letter "i". Then the function should output 2 since i is in the 2nd index spot of my vector list.

Does anyone know how to fix my error? I am not understanding why my line of code is not working.
if(is_member(list[i]==character))

In this line, you are evaluating the expression list[i]==character, and passing that value to the function is_member(). Since list[i]==character evaluates to a bool, you are therefore passing a single bool argument to the function.

However, according to the function definition (line 1), the function takes two arguments, a vector and a char. So there's an obvious mismatch.

Did you mean to pass list[i] as the first argument, and character as the second?
Last edited on
list[i]==character
This is a conditional expression. It evaluates to either true (if the two values are equal) or false (if they're not).

true and false are of type bool. Therefore that expression results in a bool.



is_member(list[i]==character)

is_member is a function. You are calling the function and giving it parameters by putting them in the (parenthesis). The compiler thinks you are giving 1 bool paramters because as previously mentioned... list[i]==character evaluates to a bool -- it's either going to be true or false.

Therefore the compiler is looking for a form of is_member which takes 1 bool as a parameter.


You do not have such a form. Your is_member function takes 2 parameters: a vector<char> and a char.

So you are calling is_member incorrectly. You need to give it 2 parameters. The parameters must be separated by a comma.

HOWEVER

You don't want to call is_member anyway. It doesn't do what you want here. The proper solution is probably just to change line 20 to this:

if(list[i]==character)




Also... the logic in is_member is broken and will only examine the first character in the given vector. The rest will be ignored.


EDIT: doh... ninja'd
Last edited on
EDIT: doh... ninja'd

Hey, just for once, I get to do it to someone else :)
I would need to pass it as two parameters! I've never worked with a Boolean with two parameters so this is tricky for me. I want to pass in my condo vector and also my character but I'm confused on how to do it.
I want to pass in my condo vector and also my character but I'm confused on how to do it.


I really don't think you do.

If you're sure that's what you want... you'd do this:

 
if( is_member( list, character ))


Note the first parameter is 'list' (a vector)
The second parameter is 'character' (a char)
they're separated by a comma.

This will compile... but it will not do what you want.

is_member will check to see if the character is ANYWHERE in the vector.. but you want to see if it's at this certain position in the vector. Therefore is_member is of no use to you here.

Again... you probably just want to do this:

if(list[i]==character)
If I do that, then I don't understand why we even make the Boolean operation
Neither do I. Is it a requirement for the assignment? Maybe you're interpreting it incorrectly?
Well the point is to enter in a string and it counts how many vowels are in the string. I have a vowel vector and also a number of times each vowel appears. For example, I will enter:

Hello world

Vowel vector: a e I o u y
Frequency vector: 0 1 0 2 0 0

My Boolean checks to make sure that the point in the vector is a vowel, which it loops through the vowel vector. Then my
Next function finds the index and increments it by one until the whole string has been checked. I have my code working until this point. (Which is pretty much the last step)
Well the point is to enter in a string and it counts how many vowels are in the string. I have a vowel vector and also a number of times each vowel appears.


Okay.. for that, is_member would be useful. is_member just isn't useful for finding the index.
Gotcha, I got it to work. Thanks for the help.
Topic archived. No new replies allowed.