PDA

View Full Version : probleme with cast from double to QString



hbill
16th May 2008, 00:25
hi
i have a C++ class that evaluate an arithmitic expression like 9+8-(9/5)
in the consol i have a good result.
but if i instantiate my class in a Qt project and pute the result in QLineEdit i have 0

a slot that instantate and calclate the expression :


void calculatrice::evaluateClicked()
{
arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
QString st;
st.setNum( e->evluate() );
outputeLE->setText( st );
}evluate() return a double

thanks
++

aamer4yu
16th May 2008, 05:39
What does ur arithmeticExpression class take in ctor arguments ??

JimDaniel
16th May 2008, 05:46
Assuming your class does return the correct value (have you checked to make sure?) setting the text of QLineEdit this way ought to work, although I would probably use QString's static number() function to make it cleaner, like this:

arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
outputeLE->setText(QString::number(e->evaluate()));

The only real problem I see here is a memory leak.

maverick_pol
16th May 2008, 07:35
Hi,

I have also checked a simple example:


double value = 9+8-(9/5);
QString st;
st.setNum(value);
qDebug()<<st;

Output: "16".
I would suggest checking the "evaluate" method. Maybe it do not remove/recognize one of the operational signs, etc.
Maybe you could paste the "evaluate" method body?

jpn
16th May 2008, 07:44
I have also checked a simple example:


double value = 9+8-(9/5);
QString st;
st.setNum(value);
qDebug()<<st;

Output: "16".

That's because you calculate with integers. Storing the result to a double doesn't fix the rounding errors. You should rewrite the division part as "(9/5.0)", "(9.0/5)" or "(9.0/5.0)".

hbill
16th May 2008, 15:39
What does ur arithmeticExpression class take in ctor arguments ??

it take std::string


Assuming your class does return the correct value (have you checked to make sure?) setting the text of QLineEdit this way ought to work, although I would probably use QString's static number() function to make it cleaner
i have the same probleme on using QString::number()


void calculatrice::evaluateClicked()
{
arithmeticExpression *e = new arithmeticExpression( inputeLE->text().toStdString() );
outputeLE->setText( QString::number(e->evluate()) );
}
:(

i have the good resulte printed in kdevelop output when i click on evaluate button :confused:

http://images.imagup.com/03/1210339216_snapshot8.png

thaks
++

JimDaniel
16th May 2008, 16:21
Have you stepped this through in a debugger or by other means to see exactly where the algorithm goes wrong? Seems like you could easily narrow it down a bit.

Apparently the slot is being called, as e->evaluate() is being called, and apparently it calculates the correct value as seen in your output window. But are you completely sure it returns the correct value to the function? Add an intermediary double variable to the function to hold the return value, then see what that is:

double d = e->evaluate();
//check what 'd' is...

I'm with the others who think it must be a bug in evaluate().

And if you have to create an object on the heap in this case, add this to the end of your function:

delete e;
e = 0;

hbill
16th May 2008, 23:00
Have you stepped this through in a debugger or by other means to see exactly where the algorithm goes wrong? Seems like you could easily narrow it down a bit.

Apparently the slot is being called, as e->evaluate() is being called, and apparently it calculates the correct value as seen in your output window. But are you completely sure it returns the correct value to the function? Add an intermediary double variable to the function to hold the return value, then see what that is:

double d = e->evaluate();
//check what 'd' is...

I'm with the others who think it must be a bug in evaluate().

And if you have to create an object on the heap in this case, add this to the end of your function:

delete e;
e = 0;

than kyou for all :)

in my fonction

double arithmeticExpression::evluate()i make

double d = pileDouble.getTopData() ;
return d;it work now :p
it was

return pileDouble.getTopData();thank you
++