title: Keeping track of which is the smallest and which is the largest value so far in a loop
body: <p>could you please help me with solving simple problem? I am very fresh with C++ and learning from book "Programming: Principles and Practice Using C++ by Bjarne Stroustrup". I have never learnt C++ before so I am not familiar with many useful features. The drill says:</p>
  
  <blockquote>
    <p>"6. Now change the body of the loop so that it reads just one double
    each time around. Define two variables to keep track of which is the
    smallest and which is the largest value you have seen so far. Each
    time through the loop write out the value entered. If it’s the
    smallest so far, write the smallest so far after the number. If it is
    the largest so far, write the largest so far after the number"</p>
  </blockquote>
  
  <p>I do not know how to do this correctly without using vector. Here is my code:</p>
  
  <pre><code>#include "C:/std_lib_facilities.h"
  
  int main()
  {
      double a, b,differ=0;
      char c=' ';
      cout &lt;&lt; "Enter two values: \n";
      while (c != '|' &amp;&amp; cin &gt;&gt; a &gt;&gt; b )
      {
          if (a &gt; b)
          {
              cout &lt;&lt; "The smaller value is: "&lt;&lt; b &lt;&lt; " and the larger value is: " &lt;&lt; a &lt;&lt; "\n \n";
              differ = a - b;
              if (differ &lt; 1.0 / 100)
                  cout &lt;&lt; "Numbers are almost equal\n\n";
          }
          else if (a &lt; b)
          {
              cout &lt;&lt; "The smaller value is: " &lt;&lt; a &lt;&lt; " and the larger value is: " &lt;&lt; b &lt;&lt; "\n \n";
              differ = b - a;
              if (differ &lt; 1.0 / 100)
                  cout &lt;&lt; "Numbers are almost equal\n\n";
          }
          else
          {
              cout &lt;&lt; "These values are equal!\n";
          }
          cout &lt;&lt; "Enter a character | to break loop: \n";
          cin &gt;&gt; c;
      }
      cout &lt;&lt; "You have exited the loop.\n";
      keep_window_open();
  }
  </code></pre>
  
  <p>And here are previous steps, these I have solved with code above:</p>
  
  <blockquote>
    <ol>
    <li>Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the
    program when a terminating '|' is entered.
    
    <ol start="2">
    <li>Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the
    larger value.</li>
    <li>Augment the program so that it writes the line the numbers are equal (only) if they are equal.</li>
    <li>Change the program so that it uses doubles instead of ints.</li>
    <li>Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two
    numbers differ by less than 1.0/100.</li>
    </ol></li>
    </ol>
  </blockquote>
  
  <p>Could you give me some hint how to do step 6.? I had some ideas but none of them worked..</p>
  
  <p>Here is new code:</p>
  
  <pre><code>#include "C:/std_lib_facilities.h"
  
  int main()
  {
      double smallestSoFar = std::numeric_limits&lt;double&gt;::max();
      double largestSoFar = std::numeric_limits&lt;double&gt;::min();
      double a,differ=0;
      char c=' ';
      cout &lt;&lt; "Enter value: \n";
  
      while (c != '|' &amp;&amp; cin &gt;&gt; a)
      {
          if (a &gt; largestSoFar)
          {
              largestSoFar = a; 
              cout &lt;&lt;"Largest so far is: "&lt;&lt; largestSoFar &lt;&lt; endl;
          }
          else if (a &lt; smallestSoFar)
          {
              smallestSoFar = a;
              cout &lt;&lt;"Smallest so far is: "&lt;&lt; smallestSoFar &lt;&lt; endl;
          }
          else if(smallestSoFar &gt;= a &amp;&amp; a&lt;=largestSoFar)
              cout &lt;&lt; a &lt;&lt; endl;
          cout &lt;&lt; "Enter a character | to break loop: \n";
          cin &gt;&gt; c;
      }
      cout &lt;&lt; "You have exited the loop.\n";
      keep_window_open();
  }
  </code></pre>