Help in Portugol (se e senao)

Asked

Viewed 141 times

0

I want the user to type "0" to have the program closed at the same time,because 0 is an invalid number, but I can’t,:

programa
{

    funcao inicio()
    {
        inteiro base,altura,area
        escreva("\nDigite a base:")
        leia(base)
        escreva("\nDigite a altura:")
        leia(altura)
        se((base == 0) ou (altura ==0)){
            escreva("\nNúmero inválido")
            pare
        } senao se((base >0) e (altura >0)){
        area = base*altura
        escreva("\n A área do retângulo é:",area)           
        }
    }
}
  • Does the comparison within se differ if it is an integer or if it is a string? Type 0 is different from "0".

  • You want to stay in an endless loop by calculating while the user does not enter an invalid number or perform only once and if the guy type 0 (zero) you enter the incorrect number and do not calculate?

  • Run only once, and if the user type 0 (zero) appears "Invalid option, try again" and terminate the program. But if you want to show me both ways,.

  • Edward Ramos,has difference yes,I declared as integer both the base variable and height,therefore will only accept numbers.

2 answers

2

Torricelli, to keep the program running until the user enters the invalid number, the code can be as follows:

programa
{

    funcao inicio()
    {
        inteiro base,altura,area
        logico continua = verdadeiro

        enquanto(continua) {
            escreva("\nDigite a base:")
            leia(base)
            escreva("\nDigite a altura:")
            leia(altura)

            se ((base == 0) ou (altura ==0)) {
                escreva("\nNúmero inválido")
                continua = falso
            } senao se ((base >0) e (altura >0)) {
                area = base*altura
                escreva("\n A área do retângulo é:",area)           
            }
        }
    }
}

If you don’t need to keep the loop, the code hardly changes, as any of the invalid numbers no longer allow you to execute the calculation:

programa
{

    funcao inicio()
    {
        inteiro base,altura,area

        escreva("\nDigite a base:")
        leia(base)
        escreva("\nDigite a altura:")
        leia(altura)

        se ((base == 0) ou (altura ==0)) {
            escreva("\nNúmero inválido")
        } senao se ((base >0) e (altura >0)) {
            area = base*altura
            escreva("\n A área do retângulo é:",area)           
        }
    }
}

And to exit as soon as the user enters the first invalid number, it can be as follows, creating a simple function that validates the typed number:

programa
{

    funcao logico validaValorDigitado(inteiro valor) {
        se (valor == 0) {
            escreva("\nNúmero inválido")
            retorne falso
        } senao {
            retorne verdadeiro
        }
    }

    funcao inicio()
    {
        inteiro base,altura,area

        escreva("\nDigite a base:")
        leia(base)

        se (nao validaValorDigitado(base)) {
            retorne
        }

        escreva("\nDigite a altura:")
        leia(altura)

        se (nao validaValorDigitado(base)) {
            retorne
        }

        area = base*altura
        escreva("\n A área do retângulo é:",area)
    }
}

All the codes were based on what you posted, I hope it helps you!

  • It worked, thank you very much!

0

in place of stop write the command interrupt at the line below the command type(" invalid number").

Browser other questions tagged

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