Thread: BOOL ,I am fool?!!

  1. #1
    Unregistered
    Guest

    Question BOOL ,I am fool?!!

    Hi,
    I have just started self-learning vc++.
    I want to show output "true" instead of 1 and "false" instead of 0.

    I searched in the forums for getting an hint but invain...Please helpp??

    My code is

    //minor problem showing 1 or 0 rather than true or false//
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int multiple(int,int);//funcn prototype

    char main()
    {
    int num,num1;


    cout<<"Enter 1st number"<<endl;
    cin>>num;
    cout<<"enter 2nd number"<<endl;
    cin>>num1;

    cout<<"second is multiple of first number"<<endl;
    cout<<multiple(num,num1);

    return 0;
    }

    int multiple(int x,int y)//funcn defn
    {
    int result;
    char a;

    if((y%x)==0) //to find even no.

    {
    result=true;
    a=static_cast<char>(result);
    //converting it into char


    cout<<"true"<<endl;
    }
    else if((y%x)!=0)
    {
    result=false;
    a=static_cast<char>(result);
    //converting it into char
    cout<<"false"<<endl;
    }

    return a;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    On a real quick read, result should be type bool, not int. Also, the function multiple returns an int, not a char; you can't return a.

  3. #3
    Unregistered
    Guest

    I agree...but

    Hi

    thanks for ur prompt reply salvelinus...

    I agree..I changed the return type of funcn from int to char..
    Output is (smiley face) instead of 1)

    I kept result int as it stores value 1 or 0 .
    though as per ur suggestion i changed it to bool,BUT no expected output.

    Any ideas????

    thanks in advance

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    string returnboolname(int bool)
    {if (bool==0)
    {return "true";
    else
    {return "false";}
    }



    Do this whenever u want the value converted.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    there is an iostream manipulator that does this.....

    boolalpha

    use it like....

    bool mybool=true;
    cout<<mybool<<endl<<boolalpha<<mybool<<endl;
    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

  6. #6
    Unregistered
    Guest
    thankx ,but its still not working .

    error C2501: 'string' : missing storage-class or type specifiers.

    I have to use some different header file for string ???

    thanks once again...

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    for stl string type.... i.e. string

    #include<string>
    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

  8. #8
    Unregistered
    Guest

    help

    Stone_Coder thankx for that hint

    But I havent came to that stuff (Boolalfa)
    without that thing how can i expect the result??
    please please help ......
    My modified version

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    #include<string>

    string multiple(int,int); //funcn prototype//

    int main()
    {
    int num,num1;


    cout<<"Enter 1st number"<<endl;
    cin>>num;
    cout<<"enter 2nd number"<<endl;
    cin>>num1;

    cout<<"second is multiple of first number"<<endl;
    cout<<multiple(num,num1);

    return 0;
    }

    string multiple(int x,int y) //funcn defn

    {
    int z;
    int bool;
    bool=y%x;

    if (bool==0)

    {
    return "true";
    }
    else

    {
    return "false";
    }

    }

    thankx..I hope u ubderstand my problem...

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<iostream> 
    using std::cin; 
    using std::cout; 
    using std::endl; 
    
    char* multiple(int,int); //funcn prototype// 
    
    int main() 
    { 
    int num,num1; 
    cout<<"Enter 1st number"<<endl; 
    cin>>num; 
    cout<<"enter 2nd number"<<endl; 
    cin>>num1; 
    
    cout<<"second is multiple of first number"<<endl; 
    cout<<multiple(num,num1); 
    
    return 0; 
    } 
    
    char* multiple(int x,int y) //funcn defn 
    
    { 
    int flag=y%x;
    
    if (flag==0) return "true"; 
    else // strictly not necessary but makes code more readable
    return "false"; 
    }
    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

  10. #10
    Unregistered
    Guest

    Thankx a Heap

    Thankx a lot .......

    really sincere thanks to all of u for taking some time for me sigh* a Newbie sigh*

  11. #11
    Unregistered
    Guest
    By the way, Stone_Coder,I forgot to ask ,
    why that asterisk is used?
    What is it??A reference,a pointer ?When should we use it normally??Can u give be 1/2 egs. for similar thing....Bec. I havent reached to the chap of pointers yet.
    Though in Funcns i read about reference its about "&" .

    I will be really grateful.....thankx

  12. #12
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    Download Teach yourself C++ in 21 days - http://members.tripod.com/~firstpod/cpp21/ch14.htm. You'll find answers for all of your questions there.
    Please excuse my poor english...

  13. #13
    Unregistered
    Guest

    Lightbulb Wonderful

    Thankx for that wondeful link...

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