title: How to check if argv[count] is an integer
body: <p>I'm trying to create a command line application in C++ and I want to make sure that the input is an integer after a certain command argument. </p>
  
  <p>For this example, I want to check if the next argument is an integer after the "-p" command argument. Here's the snippet of my code right now.</p>
  
  <pre><code>while (count &lt; argc){
      if (strcmp("-p", argv[count]) == 0){
          has_p = true; //Boolean
          pid = atoi(argv[count + 1]);
          if (pid == 0 &amp;&amp; argv[count + 1] != "0" ){
              err = 1;
              cout &lt;&lt; "pid argument is not a valid input" &lt;&lt; endl;
              pid = -1;
          }
          count++;
      }
  ...
  }
  </code></pre>
  
  <p>Now this code correctly catches the error in this inputs:</p>
  
  <ul>
  <li>-p 1777</li>
  <li>-p sss</li>
  <li>-p sss17</li>
  <li>-p [space] -U</li>
  </ul>
  
  <p>but fails at this input format</p>
  
  <ul>
  <li>-p 17sss</li>
  </ul>
  
  <p>I tried to remedy this by trying to compare it using sprintf. Unfortunately supplying sprintf with the char array pointer only outputs 1 character in buffer2. </p>
  
  <pre><code>while (count &lt; argc){
      if (strcmp("-p", argv[count]) == 0){
          has_p = true; //Boolean
          pid = atoi(argv[count + 1]);
          sprintf(buffer, "%d", pid);
          sprintf(buffer2, "%d", *argv[count + 1]);
          if (pid == 0 &amp;&amp; argv[count + 1] != "0" || (buffer != buffer2) ){
              err = 1;
              cout &lt;&lt; "pid argument is not a valid input" &lt;&lt; endl;
              pid = -1;
          }
          count++;
      }
  ...
  }
  </code></pre>
  
  <p>Is there a way to make sprintf read the whole char array? If not, is there a better solution for this apart from looping through the pointer until I hit "\0"</p>