Output condition on do-while in code C

Asked

Viewed 54 times

2

I need the user to enter the numbers 1, 2 or 3 to choose the locations, and if not select one, ask again.

But when entering the number 0 or 4, for example, it returns to the start screen (that would be the menu, destinations, costs and exit).

I need him to say "Enter a valid city code" and question again one of the three numbers.

What I did wrong?

printf("Selecione a opcao desejada:\n 1 - Destinos \n 2 - Custos \n 3 - Sair\n");
    scanf("%d", &op);
    switch(op) {
        case 1 : printf("\n1 - Destinos:\n\n Codigo da cidade      Valor da passagem por pessoa\n\n");
                 printf(" 1 - Nova York    ===>      R$3.231,00\n");
                 printf(" 2 - Londres      ===>      R$3.789,00\n");
                 printf(" 3 - Dubai        ===>      R$4.932,00\n\n");
                 printf("Informe o numero de seu destino\n");
                 scanf("%d", &destino);
                 if (destino <= 0 && destino > 3) {
                 do{
                 printf("Informe um codigo de cidade valido.\n");
                 scanf("%d", &destino);
                 } while (destino <= 0 && destino > 3);
                 }
  • 1

    It is impossible for a value to be <= 0 and simultaneously > 3. Review the definition of logic operators && e ||.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

3

That?

printf("Selecione a opcao desejada:\n 1 - Destinos \n 2 - Custos \n 3 - Sair\n");
scanf("%d", &op);
switch(op) {
case 1:
    printf("\n1 - Destinos:\n\n Codigo da cidade      Valor da passagem por pessoa\n\n");
    printf(" 1 - Nova York    ===>      R$3.231,00\n");
    printf(" 2 - Londres      ===>      R$3.789,00\n");
    printf(" 3 - Dubai        ===>      R$4.932,00\n\n");
    printf("Informe o numero de seu destino\n");
    scanf("%d", &destino);
    while (destino < 1 || destino > 3) {
         printf("Informe um codigo de cidade valido.\n");
         scanf("%d", &destino);
    }

I put in the Github for future reference.

  • 1

    Just explaining, the main idea was the change of the logical operator && by the operator || (since the user must enter the data again if the destino chosen is less than 1 or greater than 3).

  • 1

    I didn’t explain because I don’t even know if that’s what the question asks. If I’m sure, then I can complement.

Browser other questions tagged

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