tags:

views:

54

answers:

3

Possible Duplicate:
Incorrect floating point math?

Using C++ console app in visual studio.

 float percentCorrect;
 percentCorrect = ( 7 / 5 );
 printf("%f", percentCorrect);

output is 1.0000

OK so how do I get the correct decimal output??

+1  A: 

You need to include a decimal: 7.0/5.0

mikerobi
+2  A: 

7 and 5 are integers, so the compiler treats them as such and does integer division.

float percentCorrect; 
percentCorrect = (7.0/5.0); 
printf("%f", percentCorrect);

The above should print out the expected value.

Clueless
+1  A: 

7 and 5 are integer literals, so 7 / 5 performs integer division.

You need to have at least one floating literal for floating-point division to be performed:

float percentCorrect = 7.0 / 5.0;
James McNellis