C++ calculating the ratio of 2 numbers
<p>Hi guys I'm rather new to programming and working my way through Stroustrup's "Programming, Principles and Practice Using C++" and I've come to a complete standstill at the end of Chapter 3 with an exercise asking you to write a piece of code that does a number of calculations involving 2 numbers which includes finding the ratio of the numbers. Unfortunately this hasn't been covered at all in the book and I'm tearing my hair out trying to figure it out by myself, only able to find examples of code way to advanced for my small little brain. </p>

<p>The code I have at the moment is:</p>

<pre><code> double ratio;
    if (val2 &gt; val1)
        ratio = (val2 / val1);
    if (val2 &lt; val1)
        ratio = (val1 / val2);
    cout &lt;&lt; "The ratio of " &lt;&lt; val1 &lt;&lt; " and " &lt;&lt; val2 &lt;&lt; " is 1:" &lt;&lt; ratio &lt;&lt; '\n';
</code></pre>

<p>which works fine for numbers that equate to a whole ratio (e.g. 100 and 25) however despite me setting the variable "ratio" as a double it removes any decimals from the answer in cases of non whole number ratios. Can anyone tell me where I'm going wrong?</p>