-
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;
}
-
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.
-
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
-
string returnboolname(int bool)
{if (bool==0)
{return "true";
else
{return "false";}
}
Do this whenever u want the value converted.
-
there is an iostream manipulator that does this.....
boolalpha
use it like....
bool mybool=true;
cout<<mybool<<endl<<boolalpha<<mybool<<endl;
-
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...
-
for stl string type.... i.e. string
#include<string>
-
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...
-
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";
}
-
Thankx a Heap
Thankx a lot .......
really sincere thanks to all of u for taking some time for me sigh*:( a Newbie :( sigh*
-
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
-
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.
-
Wonderful
Thankx for that wondeful link...