I'm trying to create a simple C++ calculation function. The inputs are integer, and the return value must be double. As I compile it, I got this warning :
"warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data"
Can any body please help me, why I got the "integer" as return value ?
Please see the code below ....
int m,n,o;
int pembagi;
MT4_EXPFUNC double __stdcall getLots(int a, int b, int theDigits){
if ((theDigits == 2)||(theDigits == 4)) {
pembagi = 2;
}
else {
pembagi = 3;
}
double result = ((a * b) / pembagi);
return result;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
Thank you for your reply. I still got the same error ....
I changed the calculation like this : double result = (double)a * (double)b / (double)pembagi;
And call the function like this : double aloha = getLots( (double)m, (double)n, (double)o);
I got this error/warning :
warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
You get those warnings because you call getLots with double arguments when it's definition is with ints getLots(int a, int b, int theDigits)
you can use the normal getLots(int, int, int) without the (double) in from of everything , since the conversion will be made at result.