Loop for resetting the control variable in c++ Qt Creator

Asked

Viewed 55 times

0

Good night. I’m making a program that converts numerical bases into Qt Creator using C++, and the function that converts from any base to decimal, has a loop for:

int toDeci(QString str, int base)
{
    int len = str.size();
    int num = 0;  // Initialize result
    int valor;
    int i = 0;
    int potencia;

    for (i = 0; i < len; i++)
    {
        qDebug() << "Valor de i no inicio: " << i;
        if (val(str.at(i)) >= base)
        {
            return -1;
        }
        else
        {
            qDebug() << "Inicio do loop";
            qDebug() << "Valor de i: " << i;

            valor = val(str.at(i));
            qDebug() << "Valor do caractere: " << valor;
            qDebug() << "Valor de len " << len;
            potencia = qPow(base, len-1);
            qDebug() << "Valor da potencia: " << potencia;
            num += val(str.at(i)) * qPow(base, len-1);
            qDebug() << "Valor de num: " << num;
            len--;
            qDebug() << "Fim do loop";
            qDebug() << "Valor de i no inicio: " << i;
        }
    }
    return num;
}

The problem is that the loop is running twice. I put these qDebug() to show the values of the variables, and it seems that the control variable i is restarted once, making the loop run twice, even with Len worth 1. In debug comes that:

Valor de i no inicio:  0

Inicio do loop

Valor de i:  0

Valor do caractere:  1

Valor de len  1

Valor da potencia:  1

Valor de num:  1

Fim do loop

Valor de i no final:  1

Valor de i no inicio:  0

Inicio do loop

Valor de i:  0

Valor do caractere:  1

Valor de len  1

Valor da potencia:  1

Valor de num:  1

Fim do loop

Valor de i no final:  1

How to make the loop run only once? Changing the initial value of i makes the loop not run.

1 answer

0

I implemented the val function and pasted its code into a console Qt project, but could not reproduce the same problem.

#include <qdebug.h>
#include <qmath.h>

int val(QChar c) {
    return c.digitValue();
}
int toDeci(QString str, int base)
{
    int len = str.size();
    int num = 0;  // Initialize result
    int valor;
    int i = 0;
    int potencia;

    for (i = 0; i < len; i++)
    {
        qDebug() << "Valor de i no inicio: " << i;
        if (val(str.at(i)) >= base)
        {
            return -1;
        }
        else
        {
            qDebug() << "Inicio do loop";
            qDebug() << "Valor de i: " << i;

            valor = val(str.at(i));
            qDebug() << "Valor do caractere: " << valor;
            qDebug() << "Valor de len " << len;
            potencia = qPow(base, len-1);
            qDebug() << "Valor da potencia: " << potencia;
            num += val(str.at(i)) * qPow(base, len-1);
            qDebug() << "Valor de num: " << num;
            len--;
            qDebug() << "Fim do loop";
            qDebug() << "Valor de i no inicio: " << i;
        }
    }
    return num;
}

int main(int argc, char *argv[])
{
    qDebug() << "Resultado: " << toDeci("20", 16);
    return 0;
}

Exit:

Valor de i no inicio:  0
Inicio do loop
Valor de i:  0
Valor do caractere:  2
Valor de len  2
Valor da potencia:  16
Valor de num:  32
Fim do loop
Valor de i no inicio:  0
Resultado:  32

I have no definitive answer, but I believe that in your case it has to do with the fact that in C++ a loop is not always guaranteed to be executed in sequential order. There is a possibility that your compiler optimized operations and ran the loop assuming that Len was a constant x value, which actually should be x variable. I compiled the program in the Qt 5.14.0 Desktop Mingw kit in Debug and Release, but as I have no other available kits I can’t say that for sure. My suggestion is to exchange the loop for a while, to ensure the execution order of the instructions.


    while(i < len)
    {
        valor = str.at(i).digitValue();
        potencia = qPow(base, len-1);

        if (valor >= base)
            return -1;
        else
            num += valor * potencia;

        len--;
        i++;
    }

Browser other questions tagged

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