title: How to use strtol to extract numbers from user input
body: <p>I'm trying to build a Linear Equation calculator in C. My source code is below. I know this is a coding question but to explain my issue, I need to explain a little math here. I have the user input the points in the format (x,y) and want to convert the equation into one of the following ways:</p>
  <ol>
  <li>Slope-intercept -&gt; y = mx + b</li>
  <li>Standard form -&gt; ax + by = c</li>
  <li>Point-slope form -&gt; y - y1 = m(x - x1)</li>
  </ol>
  <p>y is the point on the y-axis of the coordinate plane, x is the point on the x-axis on the coordinate plane, m is the slope of a line, b is the y-intercept of the first equation, and a, b, and c are variables in the second equation.</p>
  <p>My issue is that I'm having trouble finding a way to extract numbers from a coordinate. In my source code, I have a 2 dimensional array that holds 2 strings with a certain size. I don't know if i should use <code>strtol()</code> or something else. For example, take the point (34,89). I want to extract the 34 and 89 and plug it in to the values for finding the slope (which would be (y2 - y1) divided by (x2 - x1). So it would be (89 - y) / (34 - x). As for the second point, I want to be able to do the same thing. I would like to avoid making 4 variables for each point if possible. I would also like to avoid asking the user to enter the x value of the first point then the y value of the first point etc. etc. I want to be able to ask the user for a point, including both x and y values. Near the end of the code is unfinshed, so if i have errors, I'll figure them out as I go. The problem is near the beginning and middle.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  #define MAXC 56
  
  //void convert_equation(); Function is not defined or used. Uncomment when ready to define
  void equation_format();
  void print_dots(int n);
  
  char points[2][MAXC];
  
  int main(void) {
  
      printf(&quot;\033[0;32mInitializing Linear Equation Calculator &quot;);
      fflush(stdout);
      print_dots (3);
      putchar('\n');
  
      printf(&quot;\nEnter the 1st point on the line, format (x,y)\n&gt;&gt;&gt; &quot;);
      fflush(stdout);
      fgets(points[0], sizeof points[0], stdin);
      /** trim \n from end of string read with fgets()
       *
       *  use: stringvar[strcspn (stringvar, &quot;\n&quot;)] = 0;
       *
       *       strcspn returns the length of stringvar that does NOT include \n
       *       this allows you to simply overwrite the \n with \0 (or plain old 0)
       */
      points[0][strcspn(points[0], &quot;\n&quot;)] = 0;
  
      printf(&quot;Enter the 2nd point on the line, format (x,y)\n&gt;&gt;&gt; &quot;);
      fflush(stdout);
      fgets(points[1], sizeof points[1], stdin);
      points[1][strcspn(points[1], &quot;\n&quot;)] = 0;
  
      equation_format();
  
      return 0;
  }
  
  void equation_format() {
      char option;
      while (1) {
          printf(&quot;\n\nWhat format would you like the equation in?\n1. Slope-Intercept\n2. Standard Form\n3. Point-slope\n&gt;&gt;&gt; &quot;);
          fgets(&amp;option, 2, stdin);
          switch(option) {
              case(1):{
                  int slope, yintercept;
                  break;
              }
              case(2):{
                  break;
              }
  
              case(3):{
                  break;
              }
              default: {
                  printf(&quot;Invalid input. Try again&quot;);
                  print_dots(3);
              }
  
          }
          break;
      }
  }
  
  void print_dots(int n) {
      if (n &lt; 0)
          n = -n;
  
      while (n--) {
          sleep(1);
          putchar('.');
          fflush(stdout);
      }
  }
  </code></pre>
  <p>This is a little hard to explain so if I'm not being clear, please tell me. Thank you!</p>