Well, I'll try to explain. I have a Class named Calc that evaluate a string or const char* numeric expresion, like "2*(3/5)" or similars.
Now I am trying to build a calculator in Qt, using those class to evaluate the input of a QLineEdit.
The class works like that:
Calc(const std::string &sExpresion);
Calc(const char* cExpresion);
and
void anadirExpresion(const std::string &sExpresion);
void anadirExpresion(const char* cExpresion);
And I try to use it in Qt like that:
void Calculadora::evaluar()
{
QLocale locale;
QByteArray cadena = ui->pantalla->text().toLocal8Bit();
const char* expresionAEvaluar = cadena.data();
qDebug()<<"Cadena a evaluar: "<<cadena;
std::cout<<"Cadena: "<<expresionAEvaluar<<std::endl;
evaluador.anadirExpresion(expresionAEvaluar); //evaluador is the class Calc instance
float resultado=evaluador.CalcularExpresion();
qDebug()<<"Resultado: "<<resultado;
ui->pantalla->setText(QString::number(resultado));
}
This is a try to use the Class with a const char* expression
And this is the try with a std::string expression
void Calculadora::evaluar()
{
//std::string cadena = ui->pantalla->text().toStdString();
//qDebug()<<"Cadena a evaluar: "<<ui->pantalla->text();
std::cout<<"Cadena: "<<ui->pantalla->text().toStdString()<<std::endl;
evaluador.anadirExpresion(ui->pantalla->text().toStdString());
float resultado=evaluador.CalcularExpresion();
qDebug()<<"Resultado: "<<resultado;
ui->pantalla->setText(QString::number(resultado));
}
the problem is that I lose the data after "." character. If I have "2.3" the calculator perfoms "2" Furthermore, this problem is only in Linux, because in Qt under Windows it runs properly. And it works fine in console mode under Linux and Windows.
Also, the output in Qt console of qDebug() and std::cout show a properly string.
Suggestions?
Sorry for my poor english and thanks in advanced.
***Updating:
Definitively, I think that this error is because the function atof() compiled under Qt under linux doesn't works properly. I can sound senseless but, I was following the "2.5" value to looking for where it was changed, and it changed after that (into a member function of Calc)
EP[t].OP.operando=atof(aux);
std::cout<<"changed? "<<aux<<std::endl;
std::cout<<"changed: "<<EP[t].OP.operando<<std::endl;
Under console, EP[t].OP.operando value is "2.5" after atof() function. In Windows too. But in Linux, I get "2" value.
Now I have another try:
char cad[]="2.5";
EP[t].OP.operando=atof(cad);
std::cout<<"changed: "<<EP[t].OP.operando<<std::endl;
Under console, EP[t].OP.operando value = 2.5 , but under Qt is 2
Maybe a bug? Anyone to confirm that?
Thank you
'.', store the parts separately, and then add them to afloatvariable. This would make the code work OK regardless of Windows, Linux etc.