accraze
accraze

Reputation: 1542

conversion from 'double' to 'int' in VS2010 C++

I am using Visual Studio 2010 for the first time in my life for a school assignment.... so far it's pretty simple, yet I'm running into trouble trying to convert a 'double' to an 'int'. I understand that both trunc() and round() do not work in VS2010, however i'm not sure how else I can do this.... here's my code:

double q = double floor((p+r)/2);
q = (int) q;
mergeSort(v,p,q);

mergeSort(v,q+1,r);

merge(v,p,q,r);

i need to make the variable q an int in order to work with the rest of my code... however casting does not seem to be working as I am getting these errors:

warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data

I know these are only warnings but my code still fails... is there any other way around this?

Upvotes: 0

Views: 2326

Answers (1)

Devolus
Devolus

Reputation: 22074

If you are converting a double to an int, you wil always have a loss of data, which is what the warning is about. Since you know that this is indeed the desired result, you can ignore the warning. However, you should use a

static_cast<int>(q)

instead of just (int)q;

Upvotes: 3

Related Questions