Use of while within if

1

I have to read endless averages of students and if they are greater than or equal to 7 the student is approved, if not, he is disapproved, and if I type -1 the program stops and issues a closing message. >

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

    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;
    
asked by anonymous 22.08.2018 / 21:29

3 answers

4

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
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

Look how simple it got. One of the reasons for having difficulty is because you have not yet gotten used to writing. When you try to do everything in the right place, give the spaces you need, do not give where you do not need them, put the correct keys, everything right is even easier to visualize. I did not even try to find the error that was so confusing.

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

  • Enter the loop in a condition that you know to be true
  • Ask for the note
  • Checks if it is outside the set parameters, if it is outside it gives an error message and has it re-execute the loop without doing the rest with the command continue
  • If you keep the flow, you have to print approved or disapproved according to the note
  • Back to loop
  • Exit the loop when typing -1
22.08.2018 / 21:37
1

First, for you to read endless notes, you have to read the mean value within the while . Second, test the value if it is greater than 10 and give a message. Enjoy and think about how to validate smaller 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 ...

}
    
22.08.2018 / 21:38
0

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

See:

#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, for example the feof() function, in your case it would not be necessary, but it is still a good option in situations where it is not possible or recommended to use integer values to exit a loop.

    
26.08.2018 / 18:24