PDA

View Full Version : IF-Statement problem



ayanda83
18th November 2013, 06:49
hi everyone, i have the "if" statement below. the purpose of this 'if' statement is to check if the string 'LoginDetail@#%$^*&_FhDKooKSE' is contained in the QByteArray 'readAll', if the condition is true the 'else' part of the 'if' statement is executed otherwise whatever is in the QByteArray is displayed on screen. the else part works fine but the 'false' condition displays empty strings even though there is something to display in the QByteArray. I think this is because the statement 'socket.readAll()' is executed twice, first in the condition of the 'if' statement secondly just below the 'if' statement.
void Client::readSocket()
{

if(!socket->readAll().contains("LoginDetail@#%$^*&_FhDKooKSE"))
qDebug() << socket->readAll() <<endl;
else
qDebug() << "Login window is opened";
}

how can i fix this.

stampede
18th November 2013, 07:53
I think this is because the statement 'socket.readAll()' is executed twice, first in the condition of the 'if' statement secondly just below the 'if' statement.
Yes, so make it execute once and reuse the result.

ayanda83
18th November 2013, 08:02
thank you for your reply, I have solved the problem like below. Yeah... i admit i was lazy to think.

void Client::readSocket()
{
QByteArray tempByteArray = socket->readAll();
if(!tempByteArray.contains("LoginDetail@#%$^*&_FhDKooKSE"))
qDebug() << tempByteArray <<endl;
else
qDebug() << "Login window is opened";
}