PDA

View Full Version : String Comparision



parulkalra14
27th January 2014, 11:42
I want whenever if (while ()) comes it will print Checking condition for If Statement and whenever while (if ()) comes it will print Checking condition for While Statement. I tried this code:

if(cursor.block().text().contains("while",Qt::CaseInsensitive)&& cursor.block().text().contains("(",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for While Statement");

}
if(cursor.block().text().contains("if",Qt::CaseInsensitive)&&cursor.block().text().contains("(",Qt::CaseInsensitive))
{
ui->label->setText("Checking condition for If Statement");
}

when i entered only if () and only while () then it gives Checking condition for If Statement and Checking condition for While Statement respectively. But when i entered if (while ()) and while (if ()), it gives Checking condition for If Statement in both the cases.

anda_skoa
27th January 2014, 13:50
when i entered only if () and only while () then it gives Checking condition for If Statement and Checking condition for While Statement respectively. But when i entered if (while ()) and while (if ()), it gives Checking condition for If Statement in both the cases.

Not sure what your problem is, that is exactly what your code is supposed to do. I evalutes the first condition, finds it to be true and sets the label's text. Then it evaluates the second condition, also finds it to be true and sets the label's text to a different text.

Cheers,
_

parulkalra14
28th January 2014, 05:18
I want when i entered while ( if () ) then label text will set to Checking condition for While Statement but instead of Checking condition for While Statement, Checking condition for If Statement appears in label text.

stampede
28th January 2014, 07:06
Both conditions are true for the given input, so it is quite clear that the last one will be displayed. Switch the order of both conditional blocks or change second one to "else if...".