samedi 5 décembre 2015

C: Logical operators

In this code, the user must type his/her grade (1-10) on a subject and his/her absences(0-14) on it. If he/she gets >=5 grade and <=2 absences, the student passes.

I have a problem with the possibility that both grade and absences numbers are off the limits of the programme. Which are the "right" logical operators on this:

#include <stdio.h>
#include <stdlib.h>
int main()
{   
    int gr, ap;

    printf("GIVE YOUR SUBJECT GRADE:\n");
    scanf("%d", &gr);
    printf("GIVE YOUR SUBJECT ABSENCES\n");
    scanf("%d", &ap);

    if (ap >= 0 && ap <= 14 && gr >= 0 && gr <= 10)
    {
       if (gr >= 5 && ap <= 2)
          printf("YOU PASSED!\n");
       else if (gr < 5 && ap <=2)
          printf("FAILED DUE TO YOUR GRADE\n");
       else if (gr >= 5 && ap > 2)
          printf("FAILED DUE TO YOUR ABSENCES\n");
       else if (gr < 5 && ap > 2)
          printf("FAILED DUE TO YOUR GRADE AND ABSENCES\n");   
    }
    else 
     {                    
        if (ap >= 0 && ap <= 14 && gr < 0 || gr > 10)  
             printf("FALSE GRADE NUMBER\n");       
        else if (gr >= 0 && gr <= 10 && ap < 0 || ap > 14)
             printf("FALSE ABSENCES NUMBER\n");
//here is the problem
        else if (gr < 0 || gr > 10 && ap < 0 || ap > 14) 
             printf("FALSE ABSENCES AND GRADE NUMBERS\n");
      }     
    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire