title: Parsing user input for two values
body: <p>I have been searching for a few hours and can't seem to find the answer I'm after. My apologies if this ends up being a duplicate.</p>
  
  <p>Now my program asks the user for 2 integers. Example</p>
  
  <pre><code>Prompt: 15 24
  </code></pre>
  
  <p>I after going threw other questions so people have mentioned <code>scanf</code> and <code>fgets</code>. But each of those were for entering one number at a time, whereas I want both from the one input.</p>
  
  <p>My other issue is error checking. Whether they actually entered an integer or something invalid. Example:</p>
  
  <pre><code>Prompt: Hello World
  Prompt: foo 5
  Prompt: 4 bar
  Prompt: foo
  Prompt: 
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>int get_input(int* x, int* y) {
  
      char* p, buf[LINE_MAX];
      int n1, n2;
  
      printf("Prompt: ");
      fgets(buf, sizeof(buf), stdin);
      n1 = strtol(buf, &amp;p, 10);
      if (p == buf || *p != '\n') {
          return 0;
      }
  
      n2 = strtol(buf, &amp;p, 10);
      if (p == buf || *p != '\n') {
          return 0;
      }
  
      *x = n1;
      *y = n2;
  
      return 1;
  }
  
  int main(int argc, char** argv) {
      int x, y;
      if (get_input(&amp;x, &amp;y)) {
          printf("Worked: %d %d\n", x, t);
      } else {
          printf("Invalid\n");
      }
      return 0;
  }
  </code></pre>
  
  <p>Some of the error checking is missing, but just really stuck on getting the second value if there is one. Also checking if there are more than 2 values.
  Much help would be appreciated.</p>
  
  <p>References that got me this far:</p>
  
  <p><a href="https://stackoverflow.com/questions/26583717/how-to-scanf-only-integer">SO Question</a></p>
  
  <p><a href="http://en.cppreference.com/w/c/string/byte/strtol" rel="nofollow noreferrer">strtol doc</a></p>