Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>weight</code> is calling <code>height</code> <em>again</em>, and it will obviously be generating a different number (that's the whole point of an RNG :) ).</p> <p>To obtain the effect you want you could:</p> <ul> <li><p>change <code>weight</code> to accept the the <code>height</code> as a parameter; then, in the <code>main</code>, at each iteration save the value returned by <code>height</code> in a temporary variable and pass it to height to obtain the corresponding height;</p> <pre><code>int weight(int height) { return height*2.77; } // ... inside the main ... while (n &lt;= 10) { int curHeight=height(); cout &lt;&lt; curHeight &lt;&lt; " and " &lt;&lt; weight(curHeight) &lt;&lt; endl; ++n; } </code></pre></li> <li><p>move <code>height</code> and <code>weight</code> to a class, which will store <code>height</code> as a private field, adding a <code>nextPerson</code> member that will update the internal field to a new random value.</p> <pre><code>class RandomPersonGenerator { int curHeight; public: RandomPersonGenerator() { nextPerson(); } int height() { return curHeight; } int weight() { return height()*2.77; } void nextPerson() { curHeight=random(1, 24, 60); } }; // ... inside the main ... RandomPersonGenerator rpg; while (n &lt;= 10) { rpg.nextPerson(); cout &lt;&lt; rpg.height() &lt;&lt; " and " &lt;&lt; rpg.weight() &lt;&lt; endl; ++n; } </code></pre></li> </ul> <p>(by the way, it's <code>int main</code>, not <code>void main</code>, and a <code>for</code> cycle is more appropriate than <code>while</code> in this situation)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload