Help ... double return as int ??

Hi,

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 ....

Thanks
Gio

=============================================================================


// third.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "third.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define MT4_EXPFUNC __declspec(dllexport)

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

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
{

double aloha = getLots(m,n,o);
return aloha;
}

return nRetCode;
}

You should probably cast a and b to double.

For example
1
2
3
4
double average (int a, int b)
{
	return (a*b) / 2 ;
}


Would return an int , but ...

1
2
3
4
double average (int a, int b)
{
	return ((double)a*(double)b) / 2 ;
}


will return a double :)

Here you got more on this subject : http://www.cplusplus.com/doc/tutorial/typecasting/
Hi TwoOfDiamonds,

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


Thanks
Gio
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.
Hi TwoOfDiamonds,

Thanks a lot. I will try it again ....

BR,
Gio
No problem ^_^
Topic archived. No new replies allowed.