title: drawing triangle in c
body: <p>I'm currently working on an exercise to draw a triangle from a C program. The user inputs the height of the triangle in the command line and the triangle is printed with "*"s.</p>
  
  <p>For example, an input of 3 will output:</p>
  
  <pre><code>  *
   ***
  *****
  </code></pre>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, const char *argv[]) {
  
      //initialize height variable and bottom variable
      int height, limit;
      //take command line argument and save it as an int. This ensures
      height = atoi(argv[1]);
  
      printf("You have chosen to draw a triangle of height: %d\n", height);
  
      if (height&lt;1) {
          printf("ERROR: Height too small.\n");
          exit(1);
      }
      else if (height&gt;100) {
          printf("ERROR: Height too large.\n");
          exit(1);
      }
      for (int i = 1; i &lt;= height; i++)
      {
              limit=0;
  
          // this 'for' loop will take care of printing the blank spaces
          for (int j = 1; j &lt;= height; j++)
          {
              printf(" ");
          }
          //This while loop actually prints the "*"s of the triangle.
          while(limit!=2*i-1) {
              printf("*");
              limit++;
          }
          limit=0;
          //We print a new line and start the loop again
          printf("\n");
      }
  
  return 0;
  }
  </code></pre>
  
  <p>I have two problems: </p>
  
  <ol>
  <li>The triangle produced by the program has the correct number of *'s, and is offset the correct number of spaces, but only produces a half triangle.</li>
  </ol>
  
  <p>This is the current output of my program:</p>
  
  <pre><code>  *
    ***
    *****
  </code></pre>
  
  <ol start="2">
  <li>I'm not sure how to form the if statement to catch if the user enters something that is not an integer. Using atoi() casts the input as an integer, but if the user enters, say, "yes", the error that the height is too small will be thrown. How can I work around this?</li>
  </ol>