Variable appears as undeclared when trying to use it in the "while" loop

Asked

Viewed 165 times

3

When trying to create a program that converts Celsius into Fahrenheit to get practical and I come across the following error message:

Erro "sair " undeclared (first use in this function)

My intention as the do while was to create an output option for the user. The rest of the code at first works as expected.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    do
    {
        int sair;
        float temp,formula;
        puts("Graus Celsius:");
        scanf("%f",&temp);
        system("cls");


        formula = temp * 1.8 + 32;
        printf("\nCelsius:%.1f\n\nFahrenheit:%.1f\n\n",temp,formula);

        puts("Digite 1 para sair ou 2 para uma nova conversao...\n");
        scanf("%d", &sair);
    }
    while(sair == 1);
    {
        return(0);
    }
}

2 answers

2

The difference between the while "normal" and the do while is that in the do while the loop will always be executed at least once.

Thus, the test variables have to be declared before the loop starts, otherwise it will not be able to do the checks to run this initial cycle, which is your problem.

The following flow illustrates well what happens in this type of loop:

inserir a descrição da imagem aqui

To solve just declare the sair out of the loop.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int sair = 0;
    do
    {
        float temp,formula;
        puts("Graus Celsius:");
        scanf("%f",&temp);
        system("cls");


        formula = temp * 1.8 + 32;
        printf("\nCelsius:%.1f\n\nFahrenheit:%.1f\n\n",temp,formula);

        puts("Digite 1 para sair ou 2 para uma nova conversao...\n");
        scanf("%d", &sair);
    }
    while(sair == 1);
    {
        return(0);
    }
}

I also declared the sair as 0. Just to make the code more intelligible

2


The problem is that the while is not inside the block between keys, it is outside, so it only sees variables that have been declared outside. Study on scope. Also has a block after the while that makes no sense, even though it works as expected.

I don’t like variables that current as flags, and this variable sair ends up being one when declaring out. It’s always best to keep variables in the smallest possible scope. These cases do not make much difference, but in more complex cases can help to become more readable. This case the variable sair should only be used to store the value and make a decision inside the block, so it’s best to declare it inside, all right, but you can’t use the while as you used, so come out explicitly with a if same (good programmers are detail-oriented and always make the simplest):

#include <stdio.h>

int main(void) {
    while (1) {
        float temp;
        puts("Graus Celsius:");
        scanf("%f", &temp);
        printf("\nCelsius: %.1f\nFahrenheit: %.1f\n", temp, temp * 1.8 + 32);
        puts("Digite 1 para sair ou 2 para uma nova conversao...\n");
        int sair;
        scanf("%d", &sair);
        if (sair == 1) break;
    }
}

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

  • Why you don’t like variables used as a flag?

  • I avoid state, avoid state mutation, avoided larger scope that should be in state. Flag is always an avoidable state, so I avoid. State and its mutation is the cause of most bugs. In functional languages there is no way to have flags. Flag seems to be gambiarra because he can’t find the right condition. This is a case that is not the most serious, but it is still ugly to have to declare the variable out when you do not need it out of the block (for me it is until error of the language not accept so). Some think there shouldn’t even be do-while and always be like I did, when it has at least 1 exec.

Browser other questions tagged

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