title: Segmentation Fault (core dump) when using getchar() in C?
body: <p>I am writing a program for a class, and need to recieve a string from standard input. I am using getchar(), but when I run the program it seems to produce the segmentation fault error when encountering the loop with getchar() in it. Here's my code:</p>
  
  <p>(An example input would be: 5, 14:09:01, 1, 1013)</p>
  
  <pre><code>int i, j;
  int continueInput = 1;
  /* initialize health data records for each patient */
  
  for( i=0; i &lt; MAXPATIENTS; i++ ){
      record[i].id = i + 1;
      for( j=0; j &lt; MAXTYPES; j++ ){
          record[i].buffer[j].start = 0;
          record[i].buffer[j].end = 0;
      }
  }
  printf("Welcome to the Health Monitoring System\n\n");
  
  while(continueInput == 1){
      i = 0;
      int c;
      char input[50];
      while((c = getchar()) != '\n' &amp;&amp; c != EOF){
          input[i++] = c;
      }
      if(c == EOF){
          continueInput = 0;
          continue;
      }
      else{
          input[i] = '\0';
          int length = i;
          int commaCount = 0;
          int patientNumber, command, value;
          char time[9], valueString[6];
          int timeCount = 0, valueCount = 0;
          for(i = 0; i &lt; length; i++){
              if(input[i] == ','){
                  commaCount++;
              }
              if(commaCount == 0){
                  patientNumber = atoi(input[i]);
              }
              else if(input[i] == ' '){
                  /*do nothing*/
                  continue;
              }
              else if(commaCount == 1){
                  time[timeCount] = input[i];
                  timeCount++;
              }
              else if(commaCount == 2){
                  command = atoi(input[i]);
              }
              else if(commaCount == 3){
                  valueString[valueCount] = input[i];
                  valueCount++;
              }
          }
          valueString[valueCount + 1] = '\0';
          time[timeCount + 1] = '\0';
          switch(command){
              case 1:
              {
                  double temp = atof(valueString);
                  temp = temp / 10;
                  printf("%s: Temperature for Patient ID = %d is %f", time, patientNumber, temp);
              }
              case 2:
              {
                  int heartRate = atoi(valueString);
                  printf("%s: Heart Rate for Patient id = %d is %d", time, patientNumber, heartRate);
              }
              case 3:
              {
                  int sPressure = atoi(valueString);
                  printf("%s: Systolic Pressure for Patient id = %d is %d", time, patientNumber, sPressure);
              }
              case 4:
              {
                  int dPressure = atoi(valueString);
                  printf("%s: Diastolic Pressure for Patient id = %d is %d", time, patientNumber, dPressure);
              }
              case 5:
              {
                  int rate = atoi(valueString);
                  printf("%s: Respiration Rate for Patient id = %d is %d", time, patientNumber, rate);
              }
              case 6:
              {
                  printf("%s: Print command entered for Patient id = %d", time, patientNumber);
              }
          }
      }
  }       
  printf("\nEnd of Input\n");
  </code></pre>
  
  <p>}</p>
  
  <pre><code>#define MAXPATIENTS 5
  #define MAXREADINGS 10
  #define MAXTYPES 5
  #define MAXTIME 8
  
  /* One health type reading: timestamp + actual value */
  typedef struct{
      char timestamp[MAXTIME+1];
      int value;
  }Element;
  
  /* Circular buffer of health type readings */
  typedef struct{
      int start;  /* index of oldest reading */
      int end;    /* index of most current reading */
      Element reading[MAXREADINGS];
  }CircularBuffer;
  
  /* Patient's health chart: ID + multiple health type readings */
  typedef struct{
      int id;
      CircularBuffer buffer[MAXTYPES];
  }Chart;
  </code></pre>