title: Input string with getchar
body: <p>I am trying to read a string into a char array with a length chosen by the user. The problem is that getchar() doesn't stop reading until the user manually enters a newline by pressing enter, based on my code. I have read through other's threads, and I understand why I'm not able to do it this way, it's just completely contradictory to my assignment handout. </p>
  
  <pre><code>int chPrompt(int nchars);
  void strInput(char str[], int nchars);
  
  int main(void) {
      int nchars = chPrompt(nchars);
      char str[nchars];
  
      strInput(str, nchars);
  
      return 0;
  }
  
  int chPrompt(int nchars) {
      printf("How many chars do you need to input? &gt;");
  
      return scanf("%i", &amp;nchars);
  }
  
  void strInput(char str[], int nchars) {
      int i = 0;
  
      while((str[i] = getchar()) != '\n') {
          if(i &gt; nchars-1)
              break;
          i++;
      }
      str[i] = '\0';
  
      //Troubleshooting
      printf("%s %d", str, strlen(str));
  }
  </code></pre>
  
  <p>This is what the handout says:</p>
  
  <p>Input a string from the keyboard (spaces included) using the technique we talked 
  about (while with getchar(), not gets() , fgets()or scanf() ), 
  augmented so that it will input any amount up to, but no more than, 80 
  characters. Be sure that there’s a null in the proper location after the input.</p>
  
  <p>The technique we talked about in class was the while loop with getchar assignment to char array. </p>
  
  <p>My question: 
  My professor is very adamant about his instructions. In this handout, he is specifically telling me to input any amount up to, but no more than, 80. This is contradicting the functionality of getchar, correct? Is there any way to limit the length of a string, using this 'technique'?</p>
  
  <p>On some of the threads I found, people mentioned it might be OS dependent. So, if that matters, I am on Windows 8.1.</p>