Thread: bool functions

  1. #1
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    bool functions

    if i am using a bool function such as
    Code:
    bool isaMonkey(*)
    
    
    int main ()
    {
     
    cout << isaMonkey(*) << endl;
    
    returno
    ;
    
    //
    function parameters to determine isaMonkey...
    furry skin, curly tail, eats bananas....
    what is listed at ther astericks? true, false, both? The paramerters?

    i am being told that my isaMonkey term doesn't evaluate to a function..
    Thanks scuba22 (michele)
    ps
    isaMonkey is justanexample....but the question is a yes or no, which is why I am using bool.
    Thx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A function that returns bool is just like any other function. You pass it arguments and if those arguments evaluate to true you return true, otherwise false:
    Code:
    #include <iostream>
    
    bool isGreater ( int one, int two )
    {
      return one > two;
    }
    
    int main()
    {
      int i = 27, j = 15;
    
      std::cout<< isGreater ( j, i ) <<std::endl;
      std::cout<< isGreater ( i, j ) <<std::endl;
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    bool IsAMonkey(bool FurrySkin,bool CurlyTail,bool EatsBananas)
    {
    return (FurrySkin && CurlyTail && EatsBananas) ? true:false;
    }
    something like that?
    IsAMonkey(true,true,true) returns true. Anything else returns false. So only a monkey if has furry skin and curly tail and eats bananas.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    Thumbs up thank you

    thanks * thanks * thanks * for the quick reply
    scuba22

  5. #5
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    more probs with this...

    actually my boolquestion is : isatriangle

    so the sum of 2 sides must be greater than the third to be true


    here's my fuction prototype:
    Code:
    bool isatriangle (double, double, double);
    i start my main and get my data (numbers for 3 sides)
    call my function:
    Code:
    cout<<setw(15) << s1 << s2 << s3 <<setw(15)<<isatriangle(s1, s2, s3)

    and here's my function:
    Code:
    	//function isatriangle definition
    			//x,y,z are parameters
    			    bool isatrianle( double x, double y, double z){
    
    				
    				 bool iat = false;
    
    				 if (y + z > x)
    					iat = true;
    
    				 if (x + z > y)
    					iat = true;
    
    				 if (x + y > z)
    					iat = true;
    
    				 return iat;
    
    			}
    but something is wrong and i'm getting this error:
    unresolved external symbol "bool __cdecl isatriangle(double,double,double)" (?isatriangle@@YA_NNNN@Z)
    Debug/HW_1018.exe : fatal error LNK1120: 1 unresolved externals

    can you point out what's wrong?
    scuba22

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You misspelled the function name in your definition.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    more?

    okay..thanks...duh.

    one last question...

    here's the new prototype & fuction,
    why am I not getting a yes or no when i call the bool function?

    actually what the hey..here's the entire thing...my output is a little messed up...

    Code:
     
    #include<iostream> 
    #include<iomanip> 
    #include<fstream> 
    #include<cmath> 
    #include<string>
    using namespace std;
    
    
    bool isatriangle (double, double, double);
    double semiperimeter ( double, double, double );
    double perimeter ( double, double, double );
    double area ( double, double, double );
    
    
    
    int main()
    {
    
    double s1, s2, s3;
    
    
     ifstream infile("A:\\IF1.txt");
        if(!infile){ 
            cerr << "Cannot open input file" << endl; 
            return 1; 
        } 
     
        ofstream outfile("A:\\0F1.txt"); 
        if(!outfile){ 
            cerr << "Cannot open output file" << endl; 
            return 1; 
        } 
    
    	cout << setprecision(3); 
        cout << setiosflags(ios::fixed | ios::showpoint); 
    
    
    			cout << "\n" << endl;
    
    		
    			
    
    
    			bool Flag = true;
    			while(infile >> s1 >> s2 >> s3){
    				if(Flag==true){
    					cout<<setw(15)<<"\tSIDES"<<setw(15)<<"TRIANGLE ?"<<setw(15)<<"PERIMETER"<<setw(15)<<"AREA\n" << endl; 
    					Flag=false;
    				}
    
    				cout<<setw(15) << s1 << s2 << s3 <<setw(20)<<isatriangle(s1, s2, s3)<<setw(20)<<perimeter( s1, s2, s3 )<<setw(20)<< area(s1, s2, s3)<<"\n\n" <<endl; 
    
    
    			}
    
    
    return 0;
    }
    
    
    			//function isatriangle definition
    			//x,y,z are parameters
    			bool isatriangle( double x, double y, double z){
    
    			return ((y + z > x) && (x + z > y) && (x + y > z)) ? true:false;
    
    
    			}
    
    			//function perimeter definition
    			// a,b,c are parameters
    				double perimeter ( double a, double b, double c ){
    				cout << setprecision(3); 
    				cout << setiosflags(ios::fixed | ios::showpoint);			
    
    				double p = (a + b + c);
    
    
    				return p;
    
    			}
    
    				//function semiperimeter definition
    				// a,b,c are parameters
    				double semiperimeter  ( double a, double b, double c ){
    
    				double sp = (a + b + c )/2;
    
    				
    
    				return sp;
    
    			}
    
    				//function area definition
    				// a,b,c, and are parameters
    
    				double area ( double a, double b, double c ){
    
    				cout << setprecision(3); 
    				cout << setiosflags(ios::fixed | ios::showpoint);
    
    				double sp;
    
    				double B = sp * (sp-a)*(sp-b)*(sp-c);
    
    				double A = sqrt(B);
    
    				
    				return A;
    				
    
    
    			}

  8. #8
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    1 or 0

    i'm getting 1 or zero for my bool answer of true or false...
    also, I am trying to get the area to call the semiperimeter fuction for calculation, but am not getting asnything back..am I doing it wrong?
    Michele

  9. #9
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    1 or 0

    hey all never mind..i've figured it out thanks...
    working on one function calling another.
    thanks for your help bonkey, prelude, and 'coned stoder'
    scuba22

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Classes help
    By DarkAlex in forum C++ Programming
    Replies: 13
    Last Post: 01-26-2008, 03:00 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM