Khadijah Kamba
Khadijah Kamba

Reputation: 21

How to use If statement with a boolean function in another class

I have a class EverTrueHelper.cpp.

bool EverTrueHelper::getAndReturnUserEntry(string userEntry)
{
    
    if (userEntry == "Q" || userEntry == "q")
    {
        return true;
    }
    else
    {
        return false;


    }
    return 0;
}

and I have these lines of code in my main function

cout << "Do you want to quit? Type 'Q' or 'q' else hit Enter" << endl;
cin >> userEntry;
if (getAndReturnUserEntry = true)
{
    break;
}
else
{
    continue;
}

I have an error on the "getAndReturnUserEntry" in my if statement. I can't quite figure out how to get around it.

Upvotes: 1

Views: 90

Answers (4)

dor132
dor132

Reputation: 193

You have two problem with your main function's if statement:

  • A single =, which means assignment, which is invalid in that case.
  • You have not invoked getAndReturnUserEntry, add () at the end to actually invoke the function. After that, a test for == true is not necessary either since it will work anyway.
cout << "Do you want to quit? Type 'Q' or 'q' else hit Enter" << endl;
cin >> userEntry;
if (getAndReturnUserEntry())
{
    break;
}
// continue program...

Further more, I think a better quit checking function will be:

bool EverTrueHelper::getAndReturnUserEntry(string userEntry)
{
    return (userEntry == "Q" || userEntry == "q");
}

Upvotes: 1

astrid_coder
astrid_coder

Reputation: 151

You can try like follow:

    cin >> userEntry;
    EverTrueHelper my_helper;
    if (my_helper.getAndReturnUserEntry(userEntry)) {
        break;
    }else{
        continue;
    }

Upvotes: 1

hmoein
hmoein

Reputation: 349

bool EverTrueHelper::getAndReturnUserEntry(string userEntry)  {
    
    return ((userEntry == "Q" || userEntry == "q") ? true : false);
}



cout << "Do you want to quit? Type 'Q' or 'q' else hit Enter" << endl;
cin >> userEntry;
if (getAndReturnUserEntry(userEntry) == true)
    ; // break; doesn't make sense here
else
    ; // continue; doesn't make sense here

Upvotes: 0

Mureinik
Mureinik

Reputation: 311508

You can call a method by using parentheses (()) and passing the argument(s) in them:

cin >> userEntry;
if (getAndReturnUserEntry(userEntry))
{
    break;
}
else
{
    continue;
}

Upvotes: 2

Related Questions