What's wrong with my code?

0
algoritmo "Calculadora"

Var
num1, num2, r: real
operacao: caractere

inicio

EscrevaL("Digite: ")
leia(num1, operacao, num2)

caso operacao "+"
r <- num1 + num2

caso operacao "-"
r <- num1 - num2

caso operacao "*"
r <- num1 * num2

caso operacao "/"
r <- num1 / num2

EscrevaL("Resultado:", r)

fimalgoritmo
Basically, it does not display the correct values, and how could I do to type direct operation, type like this: 2 + 2? I tried to do it there, so much that I read three variables, and it did not work out. I had to type like this: 2 + 2 So he would recognize, but, as the form I reported above, I could not do. I entered the value 3 + 3, resulted in 9, what am I missing? How can I improve the code / correct?

    
asked by anonymous 26.12.2016 / 23:07

1 answer

0

There is an error in the structure of your code.

To implement a CASE structure in VisualG you should declare it as follows:

choose case ... endcollection

Your correct code would look like this:

algoritmo "calculadora"
var
num1, num2, r: real
operacao: caractere

inicio
// Seção de Comandos
EscrevaL("Digite: ")
leia(num1, operacao, num2)

escolha operacao

caso "+"
r <- num1 + num2

caso "-"
r <- num1 - num2

caso "*"
r <- num1 * num2

caso "/"
r <- num1 / num2

fimescolha

EscrevaL("Resultado:", r)

fimalgoritmo

Notice the escolha operacao and fimescolha sections that encapsulate the CASE structure.

    
27.12.2016 / 02:00