Problem in C IF checking

Asked

Viewed 97 times

0

I need to know if the person is approved or failed. For the concept D and frequency greater than 75 is appearing approved but was to be disapproved.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
    float frequencia;
    char nome[180],conceito[180];
    printf("Digite seu nome: ");
    scanf("%s",&nome);     
    printf ("\n Digite seu conceito final com letra MAIUSCULA:");
    scanf ("%s",&conceito);
    printf ("\n Digite o valor da sua frequencia:");
    scanf ("%f",&frequencia);       
    printf ("\n Nome do aluno:%s",nome);
    printf ("\n O valor da sua frequencia e %f",frequencia);


    if(conceito=="C" || frequencia>=75){
        printf ("\n Voce esta aprovado");
    }
    else
        printf ("\n Voce esta reprovado");
    getch ();
    return 1 ;

    if(conceito=="B" || frequencia>=75){
        printf ("\n Voce esta aprovado");
    }
    else
        printf ("\n Voce esta reprovado");
    getch ();
    return 1 ;

    if(conceito=="A" || frequencia>=75){
        printf ("\n Voce esta aprovado");
    }
    else
        printf ("\n Voce esta reprovado");
    getch ();
    return 1 ;

    if(conceito=="D" && frequencia >=75){
        printf ("\n Voce esta reprovado");
    }
    else
        printf("\n Voce esta reprovado");
    getch ();

}

2 answers

1

The first problem is that you are using the operator || (AB) in the verifications of if. When the execution comes in if(conceito=="C" || frequencia>=75) he checks if the concept is equal to C OR if the frequency is greater than or equal to 75, if any of these cases work out then he will enter this check.

Soon, when the concept is D and the frequency is greater than or equal to 75 it will enter in all ifs because you are always using the operator ||. To solve just change the operators of || (OR) for && (AND), so he will only give true in if(conceito=="C" || frequencia>=75) if the concept is C And frequency greater than or equal to 75.

The second problem is how to compare strings, you are using the operator == to compare strings but this does not work properly, I suggest you use the function strcmp() of lib string.h.

Another point I highlight in your code is that you are repeating many ifs, the way you’re doing it will check every if and execute his else and thus will present several messages of "You have failed it.", I suggest you edit using else if among its validations.

Besides, one thing I realized is that you’re running getch (); and return 1 ; after each of the ifbut there is no need for this by just executing when the checks are closed as it will only enter a if.

Your code would look like this:

if(strcmp(conceito,"C") == 0 && frequencia >= 75) {
    printf ("\n Voce esta aprovado");
} else if (strcmp(conceito,"B") == 0 && frequencia >= 75) {
    printf ("\n Voce esta aprovado");
} else if (strcmp(conceito,"A") == 0 && frequencia >= 75) {
    printf ("\n Voce esta aprovado");
} else if(strcmp(conceito,"D") == 0 && frequencia >=75) {
    printf ("\n Voce esta reprovado");
} else printf("\n Voce esta reprovado");

getch ();
  • Next error is occurring with me, I asked for help from some friends they tried to change, but it was the same as yours and when I gave D and >=75 my skirt wrong, but in your show A >=75 Failed, that with A,B and C being that it was to appear approved. (More specifically the only mistake of mine you fixed but gave error in others)

  • The checking of strings was being incorrect, already corrected

1


First of all, it doesn’t exist in C:

using namespace std;

Second, if "concept" is only a letter, because it is declared with 180 characters ???

char nome[180], conceito[180];

Third, the string comparison is wrong, it should be

if (conceito[0] == 'C')

or else

if (strncmp(conceito, "C", 1) == 0)

Fourthly the program is only making the first comparison with "C", because of the "Return" command right after the program ends without making the other comparisons.

Fifth, it would be advisable to use "getchar" which is standard C, rather than "getch" which is specific to Microsoft C.

the comparison could be made at once, so

if ((conceito[0] == 'A' || conceito[0] == 'B' || conceito[0] == 'C')
     && (frequencia >= 75))
  printf("\nVoce esta' aprovado");
else
  printf("\nVoce esta' reprovado");

getchar();
return 1 ;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.