title: Function return false values in c++
body: <p>I'm playing in Codingame with c++. I only have an intermediate level in c++.</p>
  <p>the problem that I encountered was that I have to call a function twice in order to get the right value from it :</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  long double dist(long double phi1,long double lam1,long double phi2,long double lam2){
      phi1 *= M_PI/180;
      phi2 *= M_PI/180;
      lam1 *= M_PI/180;
      lam2 *= M_PI/180;
      long double R{(pow( (phi1-phi2)*cos(lam1 + (lam2-lam1)/2) ,2)  +  pow(lam1 - lam2 , 2))} ;
      return R;
  }
  long double formatting(string n){ //the values contain a ',' instead of '.'
      long double M{0};
      int comma{0};
      for(char num: n){
          if (num ==','){
              comma += 1;
          } else{
              if (comma){
                  M += atoi(&amp;num)/(pow(10,comma));
                  comma += 1;
              }else{
                  M *= 10;
                  M += atoi(&amp;num);
              }
          }
      }
      return M;
  }
  
  int main(){   
      long double min_distance{9999999999999};
      string min_name{&quot;None&quot;};
      string LON;
      cin &gt;&gt; LON; cin.ignore();
      string LAT;
      cin &gt;&gt; LAT; cin.ignore();
      int N;
      cin &gt;&gt; N; cin.ignore();
      for (int i = 0; i &lt; N; i++) {
          string id{};
          string name{};
          string add{};
          string tel{};
          string phi{};
          string lam{};
  
          getline(cin, id,';');
          getline(cin, name,';');
          getline(cin, add,';');
          getline(cin, tel,';');
          getline(cin, phi,';');
          getline(cin, lam);
          if (min_distance &gt;= dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam)) ){
              // min_distance = dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam));
              // dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam));
              min_distance = dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam));
              min_name = name;
          }
      }
      cout &lt;&lt; min_name &lt;&lt; endl;
      cerr &lt;&lt;&quot;The min distance: &quot;&lt;&lt;min_distance&lt;&lt;endl;
  }
  </code></pre>
  <p>if I uncomment one of the lines :</p>
  <pre><code>            // min_distance = dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam));
              // dist(formatting(LON),formatting(LAT) ,formatting(phi),formatting(lam));
  </code></pre>
  <p>I get exactly the right value that I want (I mainly have problems with the value 0)</p>
  <p>the wrong output :</p>
  <pre><code>cerr : The min distance: 1.25967e-07
  cout : Maison de la Prevention Sante
  </code></pre>
  <p>the right output :</p>
  <pre><code>cerr : The min distance: 0
  cout : Cimetiere Saint-Etienne
  </code></pre>
  <p>Instead of giving you any other output, you can directly go and try it in the website : <a href="https://www.codingame.com/ide/puzzle/defibrillators" rel="nofollow noreferrer">https://www.codingame.com/ide/puzzle/defibrillators</a></p>
  <p>Thanks</p>