In this code, I need to put a limitation in every part where the user is asked to input data. The user will be asked to input a grade ranging from 50-100, and if his input is not in the range, the program will display a warning, and if the user inputted the wrong range 3 times, the program will terminate. What should be the proper way? I can't get it right when I use iteratives.

code:

#include<stdio.h>
#include<conio.h>

main ()
{
float csPrelim, prelimExam, prelimGrade;
float csMidterm, midtermExam, midtermGrade;
float csFinals, finalsExam, finalsGrade;
int username,userpass;
int a;
a = 0;
int b;
b=0;


printf("\t\t\tGrade Calculator: Version 1.0\n\n");
printf("\n\tThree unsuccessful attempts will terminate the program");


menu:
printf("\n\nUsername:");
scanf("%d", &username);
printf("Password:");
scanf("%d", &userpass);

if ((username==1234) && (userpass==5678))
   {
   printf("\aWELCOME %d\n\n", username);
   goto compute1;
   }

else
    {
    printf("Access Denied!!!\n\n\n");
    a++;
        if (a==3)
        {
        return 0;
        }

        else
        {
        goto menu;
        }
}

compute1:
printf("\n\tThree unsuccessful attempts will terminate the program"); //the input with 3x attempt part


      for (csPrelim=0; csprelim<3; csPrelim++)
      {
      printf("\nEnter The Class Standing (prelim) :");
      scanf ("%f", &csPrelim);
            if (csPrelim<=49)
            printf("Invalid Grade!");
      }






compute2:
printf("\nEnter The Class Standing (midterm) :");
scanf ("%f", &csMidterm);
      if (csMidterm<=49)
         {
          printf("Invalid Value!");
          goto compute2;
         }

      else
          {
          goto compute3;
          }

compute3:
printf("\nEnter The Class Standing (Finals) :");
scanf ("%f", &csFinals);
      if (csFinals<=49)
         {
          printf("Invalid Value!");
          goto compute3;
         }

      else
          {
          goto compute4;
          }

compute4:
printf("\nEnter The Prelim Exam (prelim) :");
scanf ("%f", &prelimExam);
      if (prelimExam<=49)
         {
          printf("Invalid Value!");
          goto compute4;
         }

      else
          {
          goto compute5;
          }

compute5: 
printf("\nEnter The Midterm Exam (midterm) :");
scanf ("%f", &midtermExam);
      if (midtermExam<=49)
         {
          printf("Invalid Value!");
          goto compute5;
         }

      else
          {
          goto compute6;
          }

compute6:
printf("\nEnter The Finals Exam (Finals) :");
scanf ("%f", &finalsExam);
      if (finalsExam<=49)
         {
          printf("Invalid Value!");
          goto compute6;
         }

      else
          {
          goto print;
          }

print:
prelimGrade = (0.50 * csPrelim)+ (0.50 * prelimExam);
midtermGrade = ((1.0/3.0) * prelimGrade) + ((2.0/3.0) * ((0.50 * csMidterm) + (0.50 * midtermExam)));
finalsGrade =(1.0/3.0 * midtermGrade) + ((2.0/3.0) * ((0.50 * csFinals) + (0.50 * finalsExam)));

printf("Class Standing (Prelim) : %0.2f\n", csPrelim);
printf("Prelim Exam: %0.2f\n", prelimExam);
printf("Prelim Grade: %0.2f\n", prelimGrade);
printf("\n");

printf("Class Standing (Midterm) : %0.2f\n", csMidterm);
printf("Midterm Exam: %0.2f\n", midtermExam);
printf("Midterm Grade: %0.2f\n", midtermGrade);
printf("\n");
printf("Class Standing (Finals) :%0.2f\n",csFinals);
printf("Finals Exam: %0.2f\n", finalsExam);
printf("Finals Grade: %0.2f\n", finalsGrade);
printf("\n");



if (finalsGrade>=99)
printf("1.00");
else if (finalsGrade>=96)
printf("1.25");
else if (finalsGrade>=93)
printf("1.50");
else if (finalsGrade>=90)
printf("1.75");
else if (finalsGrade>=87)
printf("2.00");
else if (finalsGrade>=84)
printf("2.25");
else if (finalsGrade>=81)
printf("2.50");
else if (finalsGrade>=78)
printf("2.75");
else if (finalsGrade>=75)
printf("3.00");
else if (finalsGrade<=60)
printf("5.00");
printf("\n");

if (finalsGrade>=50)
   printf("Passed\n\n");
else
   printf("Failed\n\n");

getch();
goto menu;
}

Well, except for some C coding problems (conio.h is not in any standard and goto is recommeded for interpreters not for compilers), you have a problem in logic here (lines 51-57):

for (csPrelim=0; csprelim<3; csPrelim++)
{
    printf("\nEnter The Class Standing (prelim) :");
    scanf ("%f", &csPrelim);
    if (csPrelim<=49)
        printf("Invalid Grade!");
}

According to your logic, your user should be asked 3 times for csPrelim independently on the input value, the code reporting an error only when csPrelim is lower than 50. In other words, you don't have else branch (which should break the for loop) of the if condition.

Another logic problem is what if the user enters a number higher than 100? You have no control there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.