How to control the flow when an invalid value is entered?

Asked

Viewed 159 times

2

In case I want a condition to repeat several times and several times I have to use the command return for any specific line? I will put the program to explain better:

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

int main()
{
    int aprovados, auxa=0, reprovados, auxr=0, notas=0, cont=0;

    while(cont<10)
    {
        printf("digite o resultado: ");
        scanf("%i", &notas);

        if(notas==1)
            auxa++;
        if(notas==2)
            auxr++;
        if(notas!=1 && notas!=2)
        {
            cont--;
            printf("esse valor nao eh valido digite 1 ou 2: ");
            scanf("%i", &notas);
            cont++;
            if(notas==1)
                auxa++;
            if(notas==2)
                auxr++;
        }

        cont++;
    }

    printf("aprovados:%i\n", auxa);
    printf("reprovados:%i\n", auxr);

    if(auxa>=8)
        printf("bonus ao instrutor\n");
    if(auxr>=7)
        printf("algo esta errado com o instrutor\n");

return 0;
}

Here’s what I want to do on the third if if I type for example 3 it asks to type another number(I know I specified in printf that is to type 1 or 2) but if I type 3 right after the counter counts 1 increment and if I repeat numbers that are not 1 or 2 (in case to do this 20 times half of those times will be incremented in the counter) have how I give a return for a line specifies every time some number other than 1 or 2 is entered?

  • It’s hard to understand the explanation. Try writing in a more organized way, separate each idea. This even helps you understand your problem. If the problem is difficult to understand in Portuguese it will be difficult to understand in C. Do you know what the return does? I don’t think you need it in the program beyond where it is already. You know the continue? I think he’s the one you want.

  • our so many things I forgot to mark sorry the delay @bigown

2 answers

3


If I understand, I think this is what you want:

#include <stdio.h>
int main() {
    int auxa = 0, auxr = 0, notas = 0, cont = 0;
    while (cont < 10) {
        printf("digite o resultado: ");
        scanf("%i", &notas);
        if (notas == 1)
            auxa++;
        if (notas == 2)
            auxr++;
        if (notas != 1 && notas != 2)
            continue;
        cont++;
    }
    printf("aprovados:%i\n", auxa);
    printf("reprovados:%i\n", auxr);
    if (auxa >= 8)
        printf("bonus ao instrutor\n");
    if (auxr >= 7)
        printf("algo esta errado com o instrutor\n");
}

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

The show has other problems, but I think it answers your question. Plus there are better ways to do the same, but I think it’s best not to try to teach you the best code style until you understand what the code does.

2

The command Return does not do what you want, that would be "jump"/"return" to a line, similar to jmp in Assembly. You can’t do this in C. O Return ends a certain function and returns its value to the output of the function.

If I understand your question, you are looking for what the bigdown answered, using the Continue

  • It is today I already understand XD but thanks for the tip I always thank this time I had forgotten kkko

Browser other questions tagged

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