While use within the if

Asked

Viewed 2,944 times

2

I have to read infinite averages of students and if they are greater than or equal to 7 the student is approved , if not, he is flunked , and if I type -1 the program to and sends a message of completion.

I’m trying to make this program difficult and also wanted to add something that says when the user type something that is not between 0 and 10 (which are the possible student grades) but I’m not getting.

    printf("Digite a media final do aluno");
    scanf("%d",&media);

    if(media>=0 && media<=10)

    while(media!=-1)
    {
    if(media>=7)
    printf(" APROVADO! \n");
    printf("Media do proximo aluno: \n");
    scanf("%d",&media);
    else {
    printf(" REPROVADO! \n");
    printf("Media do proximo aluno: \n");
    scanf("%d",&media);}

    }
    printf("Fim do programa! \n"); // quando eu digito -1

    else
    printf("Voce digitou numero invalido ");

    system("PAUSE");
    return 0;
  • this missing a %d in the first scanf this just d

  • ah yes , nor realized but I think it was not only this error rsrs

  • In fact they had a few more things, but that’s what I saw right away

3 answers

5


I imagine this is what you want:

#include <stdio.h>

int main(void) {
    int media = 0;
    while (media != -1)  {
        printf("Digite a media do final aluno: \n");
        scanf("%d", &media);
        if (media < -1 || media > 10) {
            printf("Voce digitou numero invalido ");
            continue;
        }
        printf(media >= 7 ? "APROVADO!\n" : "REPROVADO!\n");
    }
    printf("Fim do programa! \n"); // quando eu digito -1
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It looks like it’s gotten simpler. One of the reasons you’re having trouble is because you’re not used to fine writing yet. When you try to do everything in the right place, give the spaces you need, don’t give where you don’t need it, put the right keys, everything is even easier to visualize. I didn’t even try to find the mistake so confusing that it was.

The code had unnecessary repetitions and parts. The logic is simple:

  • Enters the loop in a condition you know to be true
  • Ask for the note
  • Checks if it is outside the set parameters, if it is outside gives an error message and sends the loop to be rescheduled without doing the rest with the command continue
  • If you keep the flow, you have the print approved or failed according to the note
  • Loop
  • Exits the loop when you type -1
  • Now I understand, I’ve grown the program a lot. Thank you!

  • Only another doubt, would be 'wrong' this way here ? :

  • https://imgur.com/a/6uxEaRW (better to see)

  • Wrong is not, but has duplicate code and is different from what you originally posted. I did based on what you posted.

  • @Maniero Usar o ? : really makes the code very beautiful, I have to start using!

  • 1

    @Fábiomoral just do not abuse, have time to use

  • @Maniero I’ll read a little about this, because it’s my instinct to use soon if else, thank you

Show 2 more comments

2

First, to read infinite notes, you have to read the average value inside the while.
Second, test the value if it is greater than 10 and give a message.
Take advantage and think about how to validate less than zero but not -1 :)

media = 0;
while(media!=-1)
{
    printf("Digite a media final do aluno");
    scanf("d",&media);

    // testar a media
    if (media > 10) 
    {
       printf("digite uma média entre 0 e 10");
       continue;  // volta para o começo
    }
    ... restante do código ...

}
  • Shouldn’t you test the hypothesis of being less than 0 too? In this case less than -1

  • yes, so I left in reply "Take advantage and think about how to validate less than zero but not -1", If it all works out, it’s no fun ;)

  • Ah ok, I was just reading the codes

1

For this algorithm to have infinite loops until a condition is not met (Ex: while(media != -1)) you need to introduce a repeat structure.

Behold:

#include <stdio.h>
#include <stdlib.h>

int main(void){

float media = 0;

while(media != -1)){

    system("cls");
    printf("Digite a media do aluno:");
    scanf("%f",&media);

    if(media < -1 || media > 10){
        printf("Valor invalido!\n");
        system("PAUSE");
    }
    else if(media >= 7 && media <= 10){
        printf("Aluno aprovado!\n");
        system("PAUSE");
    }
    else if(media >= 0 && media < 7){
        printf("Aluno reprovado!\n");
        system("PAUSE");
    }

 } 

 return 0;
}

There are other ways to stop a repeat loop, such as the function feof(), in your case it would not be necessary, but it is a good option in situations where it is not possible or recommended to use integer values to exit a loop.

Browser other questions tagged

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