Thread: bool function

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    bool function

    Could someone give me an example of one or a link to one.

    I'm supposed to write one for class and can't find an example in the book.

    Thanks much-

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
    bool test(int x)
    {
          if(x)
             return true;
          return false;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here's another one:
    Code:
    #include <iostream>
    using namespace std;
    
    bool canRide(int num)
    {
         if(num < 48) return false;
         else return true;
    }
    
    int main()
    {
    	int input = 0;
    	cout<<"Enter your height in inches: ";
    	cin>>input;
    	
    	if(canRide(input))
    		cout<<"Please proceed through the gates\n"
    			<<"and enjoy the roller coaster."<<endl;
    	else
    		cout<<"For safety reasons, you are not allowed\n"
    			<<"on this roller coaster. Sorry."<<endl;	
    	return 0;
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A true bool function:
    Code:
    bool Bool(bool Bool)
    {
      return Bool;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    bool is_vowel(const char& test) const
    {
         switch(toupper(test))
         {
              case 'A':
              case 'E':
              case 'I':
              case 'O':
              case 'U': return true;
    
              default : return false;
         }
    }


    Also, you can check out bool functions used in this program and this program .
    Last edited by The Brain; 04-22-2005 at 09:14 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Thanks for the input.

    How's this? I'm supposed to determine if I am at the end of a sentence. If '.' or '?' or '!', return true, else return false.

    Code:
    bool IsEndOfSentence(char MyChar){
    	while(MyChar >> MyFile){
    		switch(MyChar){
    			case '.':
    				return true;
    				break;
    			case '?':
    				return true;
    				break;
    			case '!':
    				return true;
    				break;
    			default:
    				return false;
    		}
    	}
    }

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You should tell your instructor that you have no idea how to write a "bool" function, so you wrote a "predicate" instead.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't know how the rest of your program works, but I find that while loop to be suspect.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > while(MyChar >> MyFile){
    This part should be outside the function. This should be in main(), or whatever function calls this function.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    yes, IsEndOfSentence accepts a single char and should check if that chars is '.', '!' or '?' . The Brain's example above should give you a good idea.

  11. #11
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    To sum it all up, get rid of the while loop and it should work.

    Also, you don't need the breaks in that example, because before it can reach any of the breaks it is returning from the function.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

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. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM