title: C++ Find the middle value of any inputted data type using function templates?
body: <p>I've run into a bit of snag with this it seemed simple enough at first but now I just can't seem to work it out, any help would be greatly appreciated.</p>
  
  <p>So the question is: a user inputs three values of data type either, int, float, or char.  The program then has to return the middle value.</p>
  
  <p>Here is what i have so far:</p>
  
  <p>The exercise requires that I use a function template and nothing like an array. </p>
  
  <p>Header File:</p>
  
  <pre><code>#ifndef MIDTEMP_H
  #define MIDTEMP_H
  
  template &lt;class T&gt;
  T doMiddle(T param, T param2, T param3)
  {
     if ((param&lt;param2 &amp;&amp; param&gt;param3) || (param&lt;param3 &amp;&amp; param&gt;param2) || (param == param2 == param3))
      {
         return param;
      }
     else if ((param2&lt;param &amp;&amp; param2&gt;param3) || (param2&lt;param3 &amp;&amp; param2&gt;param) || (param2 == param == param3))
      {
        return param2;
      }
     else if ((param3&lt;param2 &amp;&amp; param3&gt;param) || (param3&lt;param &amp;&amp; param3&gt;param2) || (param3 == param2 == param))
      {
        return param3;
      }
     else if (param == param2 &amp;&amp; param &lt; param3)
      {
        return param;
      }
  
  }
  
  #endif
  </code></pre>
  
  <p>This now is the code which will test my header function:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include "Middle.h"
  using std::cin;
  using std::cout;
  using std::endl;
  
  int main()
  {
  cout &lt;&lt; "Please enter 3 integers. IF input is incorrect please input until correct." &lt;&lt; endl;
  cout &lt;&lt; endl;
  int a, b, c;
  while (!(cin &gt;&gt; a &gt;&gt; b &gt;&gt; c))
  {
      cin.clear();
      cin.ignore();
  }
  cout &lt;&lt; "The Middle is: " &lt;&lt; doMiddle(a, b, c) &lt;&lt; endl;
  cout &lt;&lt; endl;
  
  //=======================================================
  
  cout &lt;&lt; "Please enter 3 floating point values.  IF input is incorrect please input until correct." &lt;&lt; endl;
  cout &lt;&lt; endl;
  float d, e, f;
  while (!(cin &gt;&gt; d &gt;&gt; e &gt;&gt; f))
  {
      cin.clear();
      cin.ignore();
  }
  cout &lt;&lt; "The Middle is: " &lt;&lt; doMiddle(d, e, f) &lt;&lt; endl;
  cout &lt;&lt; endl;
  
  //=======================================================
  
  cout &lt;&lt; "Please enter 3 characters. IF input is incorrect please input until correct." &lt;&lt; endl;
  cout &lt;&lt; endl;
  char g, h, j;
  while (!(cin &gt;&gt; g &gt;&gt; h &gt;&gt; j))
  {
      cin.clear();
      cin.ignore();
  }
  cout &lt;&lt; "The Middle is: " &lt;&lt; doMiddle(g, h, j) &lt;&lt; endl;
  cout&lt;&lt;endl;
  return 0;
  }
  </code></pre>
  
  <p>So it works decently but lets say I do this (from cmd):</p>
  
  <p>Please enter 3 integers. IF input is incorrect please input until correct.</p>
  
  <p>5
  3.2
  6
  2
  The Middle is: 2</p>
  
  <p>Please enter 3 floating point values.  IF input is incorrect please input until</p>
  
  <p>9.0
  9
  9.0
  The Middle is: -1.#IND</p>
  
  <p>Please enter 3 characters. IF input is incorrect please input until correct.</p>
  
  <p>r
  r
  r
  The Middle is: r</p>
  
  <p>why is it returning that -1.#IND value and is there some other way of doing this without those ridiculous if statements?</p>