After finding the answer to the question does not end the application

Asked

Viewed 30 times

0

The question is, the first question would be if it is reptile and then if it has hull, the answer will be turtle, but even if the answer already appears the algorithm continues, before it closed after finding the answer and I do not know what I did that now does not end (This goes for all).

In this case, that is my only doubt, why does the program not end after finding the answer to each? I’m a beginner so I don’t know any advanced commands.

#include <stdio.h>

int main () {

    char respma, respave, respreptil;
    char respcasco, respcarn;
    char respvoa, resprap, resptrop;
    char respqua, respbi, responi;

    printf("\nO animal e reptil(s/n)? ");
    respreptil = getche();

    if (respreptil=='s') {
        printf("\nTem casco(s/n)? ");

        respcasco = getche();
        if(respcasco=='s'){
            printf("\nE uma tartaruga");
        }
        else {
            printf("\nE carnivoro(s/n)? ");
            respcarn = getche();

            if(respcarn=='s'){
                printf("\nE um crocodilo");
            }
            else{
                printf("\nE uma cobra");
            }
        }
    }

    printf("\nO animal e uma ave(s/n)? ");
    respave = getche();

    if (respave=='s') {
        printf("\nVoa(s/n)? ");

        respvoa = getche();
        if(respvoa=='s') {
            printf("\nE de rapina(s/n)? ");

            resprap = getche();
            if(resprap=='s'){
                printf("\nE uma aguia");
            }
            else{
                printf("\nE um pato");
            }
        }
        else {
            printf("\nE tropical(s/n)? ");

            resptrop = getche();
            if(resptrop=='s'){
                printf("\nE um avestruz");
            }
            else{
                printf("\nE um pinguim");
            }
        }
    }

    printf("\nO animal e um mamifero(s/n)? ");
    respma=getche();

    if(respma=='s') {
        printf("\nE quadrupede(s/n)? ");

        respqua=getche();
        if(respqua=='s') {
            printf("\nE carnivoro(s/n)? ");

            respcarn=getche();
            if(respcarn=='s'){
                printf("\nE um leao");
            }
            else{
                printf("\nE um cavalo");
            }
        }
    }

    getchar();
}

1 answer

1

to terminate the program after finding a solution it is necessary to interrupt its flow somehow, finishing its execution or redirecting to the end of it.

I advise using the command Return, this command will terminate its function and returns some value, in your case, will terminate the main function main, ending its application.

To do this, simply insert the Return after displaying the correct answer.

if(respcarn=='s'){
    printf("\nE um crocodilo\n");
    return 0; // Finalizando o programa
}

OBS: The Return 0 is used to warn that the program has ended correctly.

Here is the complete program, with a few more changes as the function getche() was not recognized.

#include <stdio.h>

int main () {

    char respma, respave, respreptil;
    char respcasco, respcarn;
    char respvoa, resprap, resptrop;
    char respqua, respbi, responi;

    printf("\nO animal e reptil(s/n)? ");
    respreptil = getchar();
    getchar();


    if (respreptil=='s') {
        printf("\nTem casco(s/n)? ");

        respcasco = getchar();
        getchar();
        if(respcasco=='s'){
            printf("\nE uma tartaruga\n");
            return 0;
        }
        else {
            printf("\nE carnivoro(s/n)? ");
            respcarn = getchar();
            getchar();

            if(respcarn=='s'){
                printf("\nE um crocodilo\n");
                return 0;
            }
            else{
                printf("\nE uma cobra\n");
                return 0;
            }
        }
    }

    printf("\nO animal e uma ave(s/n)? ");
    respave = getchar();
    getchar();

    if (respave=='s') {
        printf("\nVoa(s/n)? ");

        respvoa = getchar();
        getchar();
        if(respvoa=='s') {
            printf("\nE de rapina(s/n)? ");

            resprap = getchar();
            getchar();
            if(resprap=='s'){
                printf("\nE uma aguia\n");
                return 0;
            }
            else{
                printf("\nE um pato\n");
                return 0;
            }
        }
        else {
            printf("\nE tropical(s/n)? ");

            resptrop = getchar();
            getchar();
            if(resptrop=='s'){
                printf("\nE um avestruz\n");
                return 0;
            }
            else{
                printf("\nE um pinguim\n");
                return 0;
            }
        }
    }

    printf("\nO animal e um mamifero(s/n)? ");
    respma=getchar();
    getchar();

    if(respma=='s') {
        printf("\nE quadrupede(s/n)? ");

        respqua=getchar();
        getchar();
        if(respqua=='s') {
            printf("\nE carnivoro(s/n)? ");

            respcarn=getchar();
            getchar();
            if(respcarn=='s'){
                printf("\nE um leao\n");
                return 0;
            }
            else{
                printf("\nE um cavalo\n");
                return 0;
            }
        }
    }

    getchar();
}

OBS: I used two getchar() followed to pick up the ' n' (line break) character left in the buffer after entering.

Browser other questions tagged

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