Error in assigning a value to a variable

Asked

Viewed 803 times

1

I’m doing some algorithms on Visualg and came across the following error:

Error in assigning values to variable d: REAL to INTEGER

My code is this one:

Algoritmo "acertoMiseravi"
      d: inteiro Inicio
      escreval ("Digite dois números")
      leia (n1, n2)
      se n2 = 0 então
         escreval ("Impossivel Dividir")
      senão
         d <- (n1 / n2) //essa linha aqui tá dando erro
         escreva ("Divisão = ", d)
      fimse Fimalgoritmo

What is wrong?

2 answers

2


That’s exactly what the error message is saying. You make a division that generates a decimal part number, or the real call, and you’re trying to store that value in a variable that only fits integers, so you’ll lose the decimal part, so it’s an error. From what I understand of the code the solution is to declare d as real and not inteiro.

2

By its algorithm posted, it will not work because it is missing the declaration of the variable n1 and N2, and that is not allowed accent in SENAO and ENTAO.

As to the reason for the error cited, it is because the division will return a decimal number, and therefore you need to declare the variable d royal-type.

The correct algorithm will look like this:

Algoritmo "acertoMiseravi"
var
n1, n2: inteiro
d: real

Inicio
      ESCREVAL ("Digite dois números")
      LEIA (n1, n2)
      SE n2 = 0 ENTAO
         ESCREVAL ("Impossivel Dividir")
      SENAO
         d <- (n1 / n2)
         ESCREVAL ("Divisão = " , d)
      fimse
fimalgoritmo

A tip: if you want to round the division number, put 2:2 next to the division variable, being as follows:

ESCREVAL ("Divisão = " , d:2:2)
  • 1

    I had declared the variables, but I had taken them off the topic and I can’t remember why, kk. The error was occurring because I was using an integer type variable in a division that would result in a real number, but thank you very much for the tip and help.

  • 2

    If you want the split result to be an integer use the (integer division) operator and not / (real division).

Browser other questions tagged

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