Read two numbers in a single line in Portugol Studio

Asked

Viewed 921 times

2

I will participate in a competition in a few days, and for data entry it is necessary to make a input of more than one value on the same line. For example: Instead of the user entering the first value, press enter and inform the next one, it would inform you directly "5 2".

In C++ and in Java it is possible to do more or less like this:

int valores[2];

for(int i = 0; i<2; i++){
    cin>>valores[i];
}

But this same scheme does not work in Portugol Studio.

  • Have you seen the recommended examples on the official website? As you would in any language, you can only receive input with a tab (e.g.: 5 2), separator espaço and then use a loop to retrieve the values. Obs: I didn’t use/used Portugol Studio, I just based their documentation videos

  • Portugol is the same as for pseudo-code with read command()?

  • I don’t understand the question

  • I’m sorry I can not arrive in time for your competition, but follows the detailed answer of how to solve this problem using the Portugol Studio.

1 answer

2

In Portugol, the types of data which we use to learn programming logic are of the primitive type(making an analogy with other programming languages), that is, they do not represent a Class and, consequently, they do not have a scope with attributes and methods that are very useful. So, to solve your problem, I used the Library Texto to be able to use some functions and reach your expected result. Remember that in the code I implemented, you can enter as many values as you find necessary, separating them by space(seen in your comments).

Follows the code:

programa
{   //Considerar a biblioteca Texto como o atributo *tx*
    inclua biblioteca Texto --> tx

    funcao inicio()
    {
        cadeia respostaCompleta
        inteiro tamanho
        cadeia numero = ""

        escreva("Digite uma sequência de números, separando-os por espaço: ")
        leia(respostaCompleta)

        respostaCompleta = respostaCompleta + " "

        inteiro tamanhoResposta = tx.numero_caracteres(respostaCompleta)

        //Loop que varre os caracteres armazenados na resposta.
        para (inteiro contador = 0; contador < tamanhoResposta ; contador ++) 
        {
            //Concatena o número atual com o seguinte(para os números com mais de um dígito, ex:55, 130...), caso não tenha espaço separador.
            numero = numero + tx.obter_caracter(respostaCompleta, contador)
                se 
            (
                //Separa os números digitados por espaço, para exibí-los.
                tx.obter_caracter(respostaCompleta, contador) == ' '        
            )
            { 
                escreva("Seu número foi: ", numero, "\n") 
                numero = ""
            }
        }
    }
}

The idea was to use the function numero_caracteres of the imported library to be able to scan the response characters and check with obter_caracter if there is Espace number separators.

Note: Remember that to capture your answer, I used the datatype cadeia, once the guy inteiro as the name already says, it only accepts integer values and, space-separated numbers are not considered integers for the Portugol Studio.

inserir a descrição da imagem aqui

For that answer, I used the version v2.7.2 of Portugol Studio.

Browser other questions tagged

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