Error 'dia' undeclared (first use in this Function) what to do?

Asked

Viewed 193 times

-2

What can I do? variable not declared in function?inserir a descrição da imagem aqui

  • What went through your mind when you did validacao = dia, mes, ano>0;? Review the syntax used by the C language. You are making a return Data; but did not define the variable Data.

  • In the second row of the Data vc function you are using the operator , (comma). It works to concatenate instructions and returns the value of the last. validacao = dia, mes, ano>0 would be the same as dia != 0; mes != 0; validacao = ano > 0;, that is, n makes no sense. Right would be validacao = dia > 0 && mes > 0 && ano > 0;. In void main() you need to create the variables int dia, mes ano; and in int Data() you need to return an integer number.

1 answer

1

You have to declare the variables day, month and year within main to be able to send them as function parameters.

Validation within int Date will need to be a conditional: if(dia > 0 && mes > 0 && ano > 0) if you get into that if you give a return 1; for real and in the else one return 0; for false. On main you check if(Data(dia, mes, ano) == 1), if you enter this condition you call the function int valido that will need parameters as well.

Will stay like this:

    int data(int dia, int mes, int ano)
{
    if(dia > 0 && mes > 0 && ano > 0)
        return 1; //Os valores são válidos.
    else
        return 0; //Algum dos valores ou todos são inválidos.
}

int valido(int dia, int mes, int ano)
{
    printf("Curitiba, %i/%i/%i \n", dia, mes, ano);
    return 1;
}

int main()
{
    int dia, mes, ano;
    printf("Digite o dia, mes e ano consecutivamente: ");
    scanf("%i %i %i", &dia, &mes, &ano);
    if(data(dia, mes, ano) == 1)
    {
        printf("\nA data eh: \n\n");
        valido(dia, mes, ano);
    }
    else
        printf("\nValores invalidos. \n");
    return 0;
}

inserir a descrição da imagem aqui

Browser other questions tagged

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