C++ issue with division, multiplication, and addition on same line -


this question has answer here:

this in c++, i'm having problem program converts celsius fahrenheit.

the line i'm having issue conversion equation line:

fahrenheittemp = (9 / 5) * celsiustemp + 32 

the equation/program works fine when put 1.8 in place of 9 / 5 reason 9 / 5 never gets multiplied celsiustemp. returns value of celsiustemp + 32 only.

the rest of program good, it's 9 / 5 doesn't anything, have tried different types of combining parenthesis, etc.

your problem in fact you're using integer division instead of floating-point division. expression:

9 / 5 

evaluates to:

1 

because throws away 0.8 remainder. if want use floating-point division, you'll need make sure 1 or both of operands floating-point types:

fahrenheittemp = (9.0 / 5.0) * celsiustemp + 32; 

if farenheittemp int, you'll need cast int afterward.

fahrenheittemp = static_cast<int>((9.0 / 5.0) * celsiustemp + 32); 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -