Zach Stow
Zach Stow

Reputation: 157

Boolean function problems

Having trouble with my boolean function. When I compile the program everything runs just fine, yet when I type in "no" it still says "what can I help you with?".

#include <iostream>
#include <string> //size()
#include <cctype> //isdigit()
using namespace std; //(xxx)xxx-xxxx

bool verification(string yesOrno)
{
    if(yesOrno == "yes")return(true);
    else               return(false);
}

int main()
{
    string yesOrno;

    cout <<"Do you need more help\n";
    cin >> yesOrno;

    if(!verification(yesOrno))cout <<"What can I help you with?\n";

    return(0);
}

Upvotes: 1

Views: 112

Answers (2)

deadPix3l
deadPix3l

Reputation: 43

What happens when you type yes? Whats happening is when you type no, it returns false. Which you then reverse (!) to true. It works fine but youre flipping it, so instead of only working on "yes", it actually works on everything but " yes".

Remove the ! (Not operator) and it will work as you expect.

Upvotes: 1

Mureinik
Mureinik

Reputation: 310993

Your logic is backwards - verification returns false for anything that isn't "yes". Since "no" isn't "yes", verification("no") returns false, and in the main function you print out this message if !verification("no"), which evaluates to true.

Seems like you should drop the ! operator from the if statement.

Upvotes: 3

Related Questions