int main()
    {
          float stud1[2], stud2[2], total1, total2, ave1, ave2;
          int x;
          char letter1, letter2,opt;        

          do
          {
          system ("cls");
          printf ("Enter the test scores for Student 1\n");
          printf ("Test 1: ");
          scanf("%f", &stud1[0]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud1[0] > 100 || stud1[0] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud1[0]);       
             }
             while (stud1[0]> 100 || stud1[0] < 0);
          }
          printf ("Test 2: ");
          scanf("%f", &stud1[1]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud1[1]> 100 || stud1[1] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud1[1]);       
             }
             while (stud1[1]> 100 || stud1[1] < 0);
          }
          printf ("Test 3: ");
          scanf("%f", &stud1[2]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud1[2]> 100 || stud1[2] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud1[2]);       
             }
             while (stud1[2]> 100 || stud1[2] < 0);
          }
          total1 = stud1[0] + stud1[1] +stud1[2];    //Adds up all the makrs
          ave1 = total1/3.0;                         //Calculate the average of the marks

          //Assign a certain letter grade depending on the average marks
          if (ave1 >= 90 && ave1 <= 100)
          {
                 letter1 = 'A';
          }
          else if (ave1 >= 80 && ave1 < 90)
          {
                 letter1 = 'B';
          }
          else if (ave1 >= 70 && ave1 < 80 )
          {
                 letter1 = 'C';
          }
          else if (ave1 >= 60 && ave1 < 70)
          {
                 letter1 = 'D';
          }
          else if (ave1 >= 50 && ave1 < 60 )
          {
                 letter1 = 'E';
          }
          else if (ave1 < 50)
          {
                 letter1 = 'F';
          }

          printf ("\n");
          printf ("Now, enter the test scores for Student 2\n");
          printf ("Test 1: ");
          scanf("%f", &stud2[0]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud2[0]> 100 || stud2[0] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud2[0]);       
             }
             while (stud2[0]> 100 || stud2[0] < 0);
          }
          printf ("Test 2: ");
          scanf("%f", &stud2[1]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud2[1]> 100 || stud2[1] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud2[1]);       
             }
             while (stud2[1]> 100 || stud2[1] < 0);
          }
          printf ("Test 3: ");
          scanf("%f", &stud2[2]);
          //Error checking, when the user inputs a value less than 0 or more than 100
          if (stud2[2]> 100 || stud2[2] < 0)
          {
             do
             {
                    printf("The test is out of 100, please enter a number between 0 - 100: ");
                    scanf("%f", &stud2[2]);       
             }
             while (stud2[2]> 100 || stud2[2] < 0);
          }
          total2 = stud2[0] + stud2[1] + stud2[2];        //Adds up all of the marks
          ave2 = total2/3;                                //Calculate the average of the marks

          //Assign a certain letter grade depending on the average marks          
          if (ave2 >= 90 && ave2 <= 100)
          {
                 letter2 = 'A';
          }
          else if (ave2 >= 80 && ave2 < 90)
          {
                 letter2 = 'B';
          }
          else if (ave2 >= 70 && ave2 < 80 )
          {
                 letter2 = 'C';
          }
          else if (ave2 >= 60 && ave2 < 70)
          {
                 letter2 = 'D';
          }
          else if (ave2 >= 50 && ave2 < 60 )
          {
                 letter2 = 'E';
          }
          else if (ave2 < 50)
          {
                 letter2 = 'F';
          }
          else
          printf ("You entered incorrect values for the marks");                  

          system ("cls");
          printf("\n");
          printf("%25s", "Test 1 (%)");
          printf("%16s", "Test 2 (%)");
          printf("%16s", "Test 3 (%)");
          printf("%16s", "Average (%)");
          printf("%21s", "Letter Grade\n\n");
          printf("  Student 1");
          printf("       %.1f", stud1[0]);
          printf("             %.1f", stud1[1]);
          printf("            %.1f", stud1[2]);
          printf("            %.1f", ave1);
          printf("            %c", letter1);
          printf("\n\n");
          printf("  Student 2");
          printf("       %.1f", stud2[0]);
          printf("             %.1f", stud2[1]);
          printf("            %.1f", stud2[2]);
          printf("            %.1f", ave2);
          printf("            %c", letter2);
          printf("\n\n\n");
          printf("  Do you want to enter another set of marks (y/n)? ");
          opt = getchar();
          opt = getchar();
          }
          while (opt == 'Y' || opt == 'y');    
}

Okay, that's me entire code. And my question is, is there a way to error trap an user input to just get say a number, so it will basically keep looping if the user keep entering strings. And also, I would like to ask some feedback on my coding, what could I improve on? Thank you very much.

The simplest IMHO to solve the error problem is to get input as a string then parse the string to see that it contains all numeric digits. If it's ok then convert it to an integer by calling one of the standard conversion functions. This method has the advantage that the program will auto clear the input keyboard buffer of extraneous keys, assuming you give fgets() a large enough buffer. If the last character returned by fgets() is not the <Enter> key '\n' then you know there are more keys in the keyboard buffer. If '\n' is there, then you know you have all the keys and the keyboard input buffer is empty.

This article has been dead for over six months. Start a new discussion instead.