Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Como melhorar esse DO WHILE

Pessoal estou tentando escrever um codigo para medir temperatura e cheguei a isso aqui:

int main(void){

    int high;
    int low;
    int i;

    for (i = 0; i < NUMS; ++i) {

        do{
            printf("Enter the high value for day %d: \n", i+1);
            scanf("%d", &high);
            printf("Enter the low value for day %d: \n", i+1);
            scanf("%d", &low);    

            if ((high > 40) || (low < -40) || (high < low)){
                    printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
            }
        } while((high > 40) || (low < -40) || (high < low));
     }   

    return 0;

}

Mas percebi que to repetindo a linha do If e While. Como melhorar isso?

1 resposta
solução!

Poderia ser assim:

        do{
            printf("Enter the high value for day %d: \n", i+1);
            scanf("%d", &high);
            printf("Enter the low value for day %d: \n", i+1);
            scanf("%d", &low);    
            int incorrect = ((high > 40) || (low < -40) || (high < low));
            if (incorrect){
                    printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n");
            }
        } while(incorrect);
     }

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software