I have problems with my first Qt Program (should be a simple calculator). I would like to calculate everytime when there is already an operator in my calculator display (lineEditDisplay) and i press an operator again.
My Problem is that he recognize a '+' but not a '-' or '*' and i don't know why.
For example if i press 4, then -, then 2, the display shows me 4-2, but now if i press the posted + button i get displayed 6+ insead of 2+. So it seams that it goes in the first if statement and calculate firstNumber+secondNumber.
Here is the clicked slot for the addition button:
void Calculator::on_pushButtonAddition_clicked()
{
QString inLineEditDisplay=ui->lineEditDisplay->text(); //Get whats in display
//Loop through display string
for(int i=0; i<inLineEditDisplay.length(); i++)
{
//Check if there is already a operator -> if yes then calculate first so
//we can add a new operator; first check for +
if(inLineEditDisplay[i]=='+')
{
//Get the two numbers; in front of and behind the operator
QString firstPart=inLineEditDisplay.left(i);
QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
//Change from QString to int, so we can calculate with
int firstPartAsInt=firstPart.toInt();
int secondPartAsInt=secondPart.toInt();
inLineEditDisplay=QString::number(firstPartAsInt+secondPartAsInt);
}
//Now check for -
if(inLineEditDisplay[i]=='-')
{
//Get the two numbers; in front of and behind the operator
QString firstPart=inLineEditDisplay.left(i);
QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
//Change from QString to int, so we can calculate with
int firstPartAsInt=firstPart.toInt();
int secondPartAsInt=secondPart.toInt();
inLineEditDisplay=QString::number(firstPartAsInt-secondPartAsInt);
}
//Now check for *
if(inLineEditDisplay[i]=='*')
{
//Get the two numbers; in front of and behind the operator
QString firstPart=inLineEditDisplay.left(i);
QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
//Change from QString to int, so we can calculate with
int firstPartAsInt=firstPart.toInt();
int secondPartAsInt=secondPart.toInt();
inLineEditDisplay=QString::number(firstPartAsInt*secondPartAsInt);
}
//Now check for /
if(inLineEditDisplay[i]=='/')
{
//Get the two numbers; in front of and behind the operator
QString firstPart=inLineEditDisplay.left(i);
QString secondPart=inLineEditDisplay.right(inLineEditDisplay.length()-i);
//Change from QString to int, so we can calculate with
int firstPartAsInt=firstPart.toInt();
int secondPartAsInt=secondPart.toInt();
inLineEditDisplay=QString::number(firstPartAsInt/secondPartAsInt);
}
}
inLineEditDisplay.append('+');
ui->lineEditDisplay->setText(inLineEditDisplay);
}
I tested it also with
if(inLineEditDisplay[i]==QLatin1Char('+'))
but that didn't change anything (can't see any different behaviour)
@https://stackoverflow.com/users/2422324/user2422324
calculator.cpp -> http://pastebin.com/1bsUgg3Y
calculator.h -> http://pastebin.com/F0kbkx4g
main.cpp -> http://pastebin.com/keCu6Gcr
calculator.ui -> http://pastebin.com/nTEauYAH