title: C Program Limit the integer input range
body: <p>I am a new C programming learner, I am trying to make a program which would calculate the GPA of a student on the basis of grade marks input and credits of a subject.</p>
  
  <ol>
  <li><p>The problem I am having is I want to limit the number of subjects input from 2 to 6 only. </p></li>
  <li><p>Another problem is I want to limit the user to input integer from 1 to 100 only, instead of any other keywords, special characters (EOF)</p></li>
  </ol>
  
  <p>I have put the <strong>"###" in comment line</strong> where I require these modifications. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main(void) {
      // input      user input -- hopefully a number
      // temp       used to collect garbage characters
      // status     did the user enter a number?
      // counter    for keeping track of loop repetition
      // no         no. of subjects to be entered by user.
      // credits    credits per subject 
      // grades     grades acheived in each subject (1 to 100).
      // grade_value  for holding the value of each subject grade (for ex; 80 to hundred is 4.0)
      // grade_points  Grade points for each subject (credits * grade_value)
      // sum         sum of total grade points
      int counter = 1, subjects, no, credits, grades, status, temp;
      float grade_value, grade_point, sum;
      printf("Enter number of subjects you took for current semester: ");
      status = scanf("%d", &amp; no);
  
      // ### I want to limit this integer input to be &gt;=2 &amp;&amp; &lt;=6.
  
      while (status != 1) {
          while ((temp = getchar()) != EOF &amp;&amp; temp != '\n');
          if ((temp &lt; 2) &amp;&amp; (temp &gt; 6));
          break;
          printf("Invalid input... please enter the number of subject again: ");
          status = scanf("%d", &amp; no);
  
      }
      // ### I want to be this input to block other character inputs than integer from 1 to hundred.
      while (counter &lt;= no) {
          printf("\nEnter Subject %d grades separated with credits \n", counter);
          scanf("%d %d", &amp; grades, &amp; credits);
          if ((grades &gt; 0) &amp;&amp; (grades &lt;= 29)) {
              grade_value = 0;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 30) &amp;&amp; (grades &lt;= 34)) {
              grade_value = 0.67;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 35) &amp;&amp; (grades &lt;= 39)) {
              grade_value = 1;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 40) &amp;&amp; (grades &lt;= 44)) {
              grade_value = 1.33;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 45) &amp;&amp; (grades &lt;= 49)) {
              grade_value = 1.67;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf(" \nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 50) &amp;&amp; (grades &lt;= 54)) {
              grade_value = 2;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 55) &amp;&amp; (grades &lt;= 59)) {
              grade_value = 2.33;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 60) &amp;&amp; (grades &lt;= 64)) {
              grade_value = 2.67;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 65) &amp;&amp; (grades &lt;= 69)) {
              grade_value = 3;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 70) &amp;&amp; (grades &lt;= 74)) {
              grade_value = 3.33;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 75) &amp;&amp; (grades &lt;= 79)) {
              grade_value = 3.67;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          } else if ((grades &gt;= 80) &amp;&amp; (grades &lt;= 100)) {
              grade_value = 4;
              printf("Grade value for subject %d is: %.2f", counter, grade_value);
              grade_point = credits * grade_value;
              printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
              sum = sum + grade_point;
              ++counter;
          }
          // To print a message if user doesnt enter an integer varying from 1 to 100.
          else {
              printf("\n Error Grade input, Please Key in Again. (1 to 100 only.)");
          }
      }
      printf("\n");
      printf("\n");
      printf("\nThe GPA is: %.2f", sum);
      if (sum &lt;= 49) {
          printf("\nYou can register for 2 subjects for next semester.");
      } else if ((sum &gt;= 50) &amp;&amp; (sum &gt;= 79)) {
          printf("\nYou can register for 5 subjects for next semester.");
      } else if ((sum &gt;= 80) &amp;&amp; (sum &lt;= 100)) {
          printf("\nYou can register for 6 subjects for next semester.");
      }
      printf("\n");
      printf("\n");
      printf("\n_______________________________________________________");
      printf("\nEnd of program");
      return 0;
  }
  </code></pre>