I need the replay to stop when I type "F"

Asked

Viewed 132 times

-4

#include<stdio.h>
main()
{
    int O,i;
    float F,VI=0,VF=0;
    printf("Valor Inicial:"); scanf("%f",&VI);
    printf("Valor Final:"); scanf("%f",&VF);
    while (VI!=F)
    {
        if(VI<=VF)
            O=1;
        else 
            O=-1;

        for (i=VI;i!=VF+O;i=i+O)
        {
            printf("\n %d",i);
        }
        printf("\n#############################");
        printf("\nValor Inicial:"); scanf("%f",&VI);
        printf("\nValor Final:"); scanf("%f",&VF);

    }
}
  • corrects the indentation of your program...put 4 spaces at the beginning of each line of the program

  • And what is the value of variable F? If your variables are float type what do you mean by "when I type F"?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

The question does not make clear the criteria and I had even closed it, but as there is an answer that does not actually say where it is to put this, how I preferred to answer, but in the next questions give more details of what you want to do, because I can’t guarantee that this is the best solution. It may be that negative values are not accepted, or 0. so the output could be made by a different criterion and not need the auxiliary request to close. Note that the code can be much simpler.

#include<stdio.h>

int main() {
    while (1) {
        float vi, vf;
        printf("\n#############################");
        printf("\nValor Inicial: "); scanf("%f", &vi);
        printf("\nValor Final: "); scanf("%f", &vf);
        int o = (vi <= vf) ? 1 : -1;
        for (int i = vi; i != vf + o; i += o) printf("\n %d", i);
        char continuar;
        printf("\nDigite F para encerrar: "); scanf("%c", &continuar);
        if (continuar == 'F') break;
    }
}

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

1

I recommend changing the repeating structure to while.

while(condição){

}

Compare the variable you store the input user with 'F':

while(var != 'F'){

}

The code will only stop if the variable equals F.

Basically the solution is this.

Browser other questions tagged

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