Doubts with do-while loop

Asked

Viewed 105 times

0

People I’m learning programming and I’m having doubts regarding the do-while loop, I was solving an exercise but at the time of executing the code presents a problem: does not end the loop, if I type the correct option it informs invalid option.

This is the code, I’m sorry for the mistakes, but as I said I’m learning and the only way I have to train is by cell phone, I don’t have a PC. If you find errors (I believe there should be several) I ask you to report these errors and explain to me what will be improved.

#include <stdio.h>
int main (){
    int qtdf, qtdm, qtdp, qtdc, sexo, olhos;
    // As variáveis abaixo tive que dar valor a elas pois o compilador "exigiu".
    qtdf = 0;
    qtdm = 0;
    qtdp = 0;
    qtdc = 0;
    sexo = 0;

    do {
        printf ("Informe seu sexo:\n\n");
        printf (" 1 - Feminino 2 - Masculino\n\n");
        scanf (" %d", &sexo);
        printf ("Informe a cor dos seus olhos:\n");
        printf ("1 - Pretos 2 - Claros \n\n");
        scanf (" %d", &olhos);
        if (sexo == 1){
            qtdf = qtdf + 1;
        } else if (sexo == 2){
            qtdm = qtdm + 1;
        } else {
            printf ("Opção inválida\n"); 
        }
        if (olhos == 1){
            qtdp = qtdp + 1;
        } else if (olhos == 2){
            qtdc = qtdc + 1;
        } else {
            printf ("Opção inválida\n");
        }
    } while (sexo != 0);
    printf ("Total de sexo feminino: %d\n", qtdf);
    printf ("Total de sexo masculino %d\n", qtdm);
    printf ("Total de olhos pretos: %d\n", qtdp);
    printf ("Total de olhos claros %d\n", qtdc);
    return 0;
}

print do que é exibido!

  • 1

    Invalid option appears because you entered the value 0 for sex, IE, your gender condition whether it is male or female would not enter any of them, resulting in Else.

  • I tested the code here, the loop stops usually when 0 is typed in sex. And about the "invalid option" being printed, it’s for the reason that Kevin said above.

  • To better understand, add a else if(sexo == 0){printf ("Seu texto...\n"); above the first else.

2 answers

3

The problem is that your exit condition do-while is 0. This entry of 0, in your program, is not expected in sex conditions, ie ends up resulting in invalid option.

Therefore, one way to do it is when typed sexo = 0 you break out of the loop. What’s more, if you don’t want to continue, because you have to type the color of the eyes?

#include <stdio.h>
int main() {
    int qtdf, qtdm, qtdp, qtdc, sexo, olhos;
    // As variáveis abaixo tive que dar valor a elas pois o compilador "exigiu".
    qtdf = 0;
    qtdm = 0;
    qtdp = 0;
    qtdc = 0;
    sexo = 0;

    do 
    {
        printf("Informe seu sexo:\n\n");
        printf(" 1 - Feminino 2 - Masculino\n\n");
        scanf_s(" %d", &sexo);
        printf("Informe a cor dos seus olhos:\n");
        printf("1 - Pretos 2 - Claros \n\n");
        scanf_s(" %d", &olhos);

        if (sexo == 1)
            qtdf = qtdf + 1;
        else if (sexo == 2)
            qtdm = qtdm + 1;
        // Como 0 é a condição para saída no seu do-while, fazemos um break quando digitado
        // Porque não faria sentido digitar a cor dos olhos sem querer continuar
        else if (sexo == 0)
            break;
        else if(sexo != 0)
            printf("Opção inválida\n");

        if (olhos == 1)
            qtdp = qtdp + 1;
        else if (olhos == 2)
            qtdc = qtdc + 1;
        else 
            printf("Opção inválida\n");
    } while (sexo != 0);

    printf("Total de sexo feminino: %d\n", qtdf);
    printf("Total de sexo masculino %d\n", qtdm);
    printf("Total de olhos pretos: %d\n", qtdp);
    printf("Total de olhos claros %d\n", qtdc);
    return 0;
}

Another problem is: if I type invalid sex option, the color of the eyes is still requested and if I type valid sex and the color of the eyes no, still it is added to the end.

For this reason, I would consider doing so as follows: ask for both information without adding anything and check for it. If any is invalid, we do nothing and continue the loop. We only do it at the end with all validated.

Just to illustrate and you get the idea

#include <stdio.h>

int main() 
{
    int qtdf, qtdm, qtdp, qtdc, sexo, olhos;
    // As variáveis abaixo tive que dar valor a elas pois o compilador "exigiu".
    qtdf = 0;
    qtdm = 0;
    qtdp = 0;
    qtdc = 0;
    sexo = 0;

    do 
    {
        printf("Informe seu sexo:\n\n");
        printf(" 1 - Feminino 2 - Masculino\n\n");
        scanf_s(" %d", &sexo);
        printf("Informe a cor dos seus olhos:\n");
        printf("1 - Pretos 2 - Claros \n\n");
        scanf_s(" %d", &olhos);

        if ((sexo == 1 || sexo == 2) && (olhos == 1 || olhos == 2))
        {
            if (sexo == 1)
                qtdf = qtdf + 1;
            else if (sexo == 2)
                qtdm = qtdm + 1;

            if (olhos == 1)
                qtdp = qtdp + 1;
            else if (olhos == 2)
                qtdc = qtdc + 1;
        }
        else if (sexo != 0)
            printf("Opção inválida\n");
    } while (sexo != 0);

    printf("Total de sexo feminino: %d\n", qtdf);
    printf("Total de sexo masculino %d\n", qtdm);
    printf("Total de olhos pretos: %d\n", qtdp);
    printf("Total de olhos claros %d\n", qtdc);
    return 0;
}
  • I liked the answer, but it would not be better to use %i us scanf_s?

  • 1

    Well, then I guess it’s not so relevant. I believe there will be no octal or hexadecimal reading in these entries so there is no practical difference.

  • 1

    I made the corrections informed, in the problem that I had to solve I asked to close the program when typing 0 for sex, but it does not happen that when I type 0 for sex, it is still requested the color of the eyes and then it is closed, thanks to your help I was able to notice it. I will redo the exercise and create a way to shut it down once and for all.

0

Hello, in my opinion your code is going well, what I would advise is to change the position a little and add a break so that it exits the loop as soon as the user gives an invalid option:

int main (){

 int qtdf=0, qtdm=0, qtdp=0, qtdc=0, sexo, olhos;

do {
    printf ("Informe seu sexo:\n\n");
    printf (" 1 - Feminino 2 - Masculino\n\n");
    scanf (" %d", &sexo);
    if (sexo == 1){
        qtdf = qtdf + 1;
    } else if (sexo == 2){
        qtdm = qtdm + 1;
    } else {
        printf ("Opção inválida\n"); 
        break;
    }

    printf ("Informe a cor dos seus olhos:\n");
    printf ("1 - Pretos 2 - Claros \n\n");
    scanf (" %d", &olhos);
    if (olhos == 1){
        qtdp = qtdp + 1;
    } else if (olhos == 2){
        qtdc = qtdc + 1;
    } else {
        printf ("Opção inválida\n");
        break;
    }
} while (sexo != 0);
printf ("Total de sexo feminino: %d\n", qtdf);
printf ("Total de sexo masculino %d\n", qtdm);
printf ("Total de olhos pretos: %d\n", qtdp);
printf ("Total de olhos claros %d\n", qtdc);
return 0;
}

That way when the user informs an invalid option in sex for example the program will already exit the loop without asking another option and will already show the output of the program.

What was happening before in your code was that by code sequence when reporting an invalid sex still the program continued running until the end, where there was an invalid code message, with the change in the order of the code as soon as the user enters something invalid he already immediately exits the loop and gives the message.

I hope I helped you, buddy :).

Browser other questions tagged

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