title: Implementing an alternative to scanf
body: <p>Since almost all C functions designed to get data from <code>stdin</code> are bad / flawed: </p>
  
  <ul>
  <li><p><code>gets</code> the less said about this the better</p></li>
  <li><p><code>scanf</code> no checks for buffer overflow and <code>'\n'</code> constantly remaining in <code>stdin</code>, screwing up next <code>scanf</code>s</p></li>
  <li><code>scanf_s</code> pretty much the same, but with buffer overflow checks </li>
  <li><code>fgets</code> appends a <code>'\n'</code> to the <code>string</code></li>
  <li><code>gets_s</code> doesn't have the previous problem, but useless for other streams</li>
  </ul>
  
  <p>I decided to write my own function that would at least be usable to read numbers from <code>stdin</code></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  void *scano(char mode);
  
  int main()
  {
  
    // int *num = (int *) scano(sData, 'd');
     float *num = (float *)scano('f');
     printf("Half: %f", *(num)/2);
  
  
  
      return 0;
  }
  
  void *scano(char mode){
  
      char sData[20];
      fgets(sData, 20, stdin);
      *(sData + strlen(sData) - 1) = '\0'; //get rid of the '\n' before the '\0'
     switch(mode){
         case 'd':{
             int *dataI = (int *)malloc(sizeof(int));
             *dataI = atoi(sData);
             return dataI;
         }
         case 'f':{
             float *dataF = (float *)malloc(sizeof(float));
             *dataF = atof(sData);
             return dataF;
         }
         case 'D':{
             //double...
         }
     }
  
  }
  </code></pre>
  
  <p>The function is obviously unfinished for other data types, but I have some questions first:</p>
  
  <ul>
  <li>How can the algorithm of the function be improved?</li>
  <li>Don't I need to <code>free()</code> in every <code>case</code>? I'm aware allocated memory
  needs to be freed, but when working with lists, <code>free()</code> was only
  used to delete <code>Nodes</code>, when creating <code>Nodes</code>, no <code>free()</code> was called
  after a <code>malloc()</code>.</li>
  <li>Is it fully safe? If not, how can it be made safe?</li>
  </ul>