Thread: help understanding bool

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    help understanding bool

    Code:
    #include <iostream>
    using namespace std;
    
    bool isEven(int);
    
    int main()
    {
    	int val;
    
    	cout << "Enter an integer and I will tell you ";
    	cout << "if it is even or odd: ";
    	cin  >> val;
    	
    	if (isEven(val))
    		cout << val << " is even.\n";
    	else
    		cout << val << " is odd.\n";
                
                return 0;
    }
    
    bool isEven(int number)
    {
    	bool status;
    
    	if (number % 2)
    		status = false; 
    	else
    		status = true; 
    	
    	return status;
    }
    Can someone explain the status being false or true in the function definition? I thought it would be the other way around.

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    edited out
    Last edited by nextus; 04-05-2003 at 07:37 PM.
    nextus, the samurai warrior

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    The if(number%2)
    statement equals 0 if the number is even or something else if it is odd, 0 is ineterpreted as false in an if statement, so if it's even the if statement is false, if it's not it is true.

    So if you enter 2, 2 % 2 = 0, therefore, the if statement is false, it goes to the else, isEven returns true.
    If you ever need a hug, just ask.

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    bool isEven(int number)
    {
    	bool status;
    
    	if (number % 2)
    		status = false; 
    	else
    		status = true; 
    	
    	return status;
    }
    hmmm....

    Code:
    bool isEven(int number)
    {
         return (number % 2);
    }
    Last edited by abrege; 04-05-2003 at 09:02 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    That would make it return false if it was even.
    Would:

    Code:
    bool isEven(int num)
    {
        return !num % 2;
    }
    Work?
    If you ever need a hug, just ask.

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    indeed
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM