1

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);
}
5
  • 3
    Can you tell us what !false is? Commented Jul 18, 2015 at 3:59
  • On a side note, you can make the verification evaluation a lot simpler by using return (yesOrno == "yes"); instead of adding in the extra step and complicating things unnecessarily. Commented Jul 18, 2015 at 4:20
  • 1
    @DrewDormann That answer is.... true Commented Jul 18, 2015 at 5:51
  • The comments on lines 2 and 4 are cryptic Commented Jul 18, 2015 at 5:52
  • 1
    @MattMcNabb I read that as meaning "comment 2 and 4 (to this question) are cryptic", and ... spent a good minute just appreciating such a meta-observation. Commented Jul 18, 2015 at 5:56

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.