Booleans

Hello again, I would like to know if someone could give me a rundown of how setting a basic boolean return function goes. For example; If the user enters 5 return truth. else false. But of course without using "cout>>"false" or vice versa. Thanks in advance.

P.S. Although unrelated for those wondering why I'm asking this it's just to clarify a few things, I'm not so lazy as to make someone write my code for me which I'm saddened to have discovered is what many have done and will do. I actually enjoy spending late nights constantly checking my own code over and over telling myself I know this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool checkNum(int toCheck, int against)
{
  return toCheck == against;
}

int main(void)
{
  int i = 0;
  int input;
  do
  {
    cout << "guess my number: ";
    cin >> input;
  }
  while (!checkNum(input, i) && i++ < 5);

  if (checkNum(i , 6))  cout << "I win\n";
  else cout << "You win\n;
  return 0;
} 

Last edited on
I don't know what a return function is, much less a basic boolean return function.

1
2
3
4
5
6
bool return_true_if_user_entered_5()
{
    int value = 0 ;
    std::cin >> value ;
    return value == 5 ;
}
Sorry if that didn't answer my question. What would be the basic layout?

Like for if statements we have
1
2
3
4
If(condition)
{
statement;
}
zero117 wrote:
boolean return function

A very verbose approach:
1
2
3
4
5
6
7
8
9
10
11
bool test(int n)
{
    bool result;

    if (n == 5)
        result = true;
    else
        result = false;

    return result;
}


More concisely:
1
2
3
4
bool test2(int n)
{
    return (n == 5);
}
Got it to work, thanks. I'd just like to ask how can i change if it displays the actual words "true" or "false" , instead of it's interpretation being 0s or 1s. or is that as much as I'm allowed since I asked for a bool return type?
1
2
3
4
5
6
7
8
9
10
11
12
13

 
if((num1==num7) && (num2==num6) && (num3==num5))
    {
        final=true;
    }

    else
    {
        final=false;
    }

    return final;


Just a sample of the piece I needed help with, I had everything except the bool piece, sadly I didn't remember that while I declared a bool return type I would have to declare a bool somewhere inside.
Last edited on
You are allowed to use the true/false value to control anything you wish. That's what it's there for. Just test the result using an if statement.

(or perhaps use the conditional operator).
Last edited on
LowestOne had the right idea. Internet went out, so I went over my notes and found the whole Boolalpha reference. Thank you very much either way.
Topic archived. No new replies allowed.