Problem in checking with IF in C

0

I need to tell you if the person is approved or disapproved. For 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 ();

}
    
asked by anonymous 31.10.2018 / 23:43

2 answers

1

First, this does not exist in C:

using namespace std;

Secondly, if "concept" is just a letter, because it is declared with 180 characters ???

char nome[180], conceito[180];

Third, the string comparison is wrong, should be

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

or

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

Fourthly, the program is only doing the first comparison with "C", because of the "return" command, then the program ends without making the other comparisons.

In the fifth place it would be advisable to use "getchar" which is of standard C, rather than "getch" which is specific to Microsoft C.

The comparison could be made all 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 ;
    
01.11.2018 / 00:26
0

The first problem is that you are using the || (OR) operator on if checks. When execution arrives at if(conceito=="C" || frequencia>=75) it checks whether the concept equals C OR if the frequency is greater than or equal to 75, if any of these cases is successful then it will enter that verification. p>

So when the concept is D and the frequency is greater than or equal to 75 it will go into all% s of% s because you are always using the if operator. To solve, just change the operators from || (OR) to || (AND), so it will only give true in && if the concept equals C E the frequency greater than or equal to 75.

The second problem is the way of comparing strings, you are using the if(conceito=="C" || frequencia>=75) operator to compare strings but this does not work correctly, I suggest that you use the == function of strcmp() lib.

Another point that I point out in your code is that you are repeating many% s of% s, in the way you are doing it it will check every string.h and run your if You're disapproved. ", I suggest editing using if between your validations.

In addition, I realized that you are running else and else if after each getch (); s, but there is no need for this to be done only when the checks are closed since it will only enter a return 1 ; .

Your code looks 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 ();
    
31.10.2018 / 23:57