June 1, 2011

Ruzik Ruzik
Lab Rat
293 posts

Code doesnt work

 

Hellow, plaease, tell me why my code doesnt work?

  1. bool isClass = false;
  2.     for(int a=0;a<=needNext.count();a++)
  3.     {
  4.         if (QString(needNext[a])!=QString(" "))
  5.             if (QString(needNext[a])==QString("}"))
  6.             {
  7.                 qDebug() <<"1"+ QString(needNext[a]);
  8.                 isClass=true;
  9.                 a=needNext.count();
  10.             }
  11.             else
  12.             {
  13.                 a=needNext.count();
  14.                 qDebug() <<"2"+ QString(needNext[a]);
  15.             }
  16.     }

It is must find last symbol in text unequal “ “(space) and if last symbol equal “{” should happen assignment
It is always display “2 (without second qoute)
As the text is supplied text
  1.  background-color: #123123;    
  2. }

for example

5 replies

June 1, 2011

SimonL SimonL
Lab Rat
46 posts

do you have a new line after your “ “, It may be better to use regex to find your last space
[Edit]
A good way to debug this may be to print which character you are seeing last you may want to print this as a int value

June 1, 2011

Ruzik Ruzik
Lab Rat
293 posts

I dont have new line after “ “

June 1, 2011

Ruzik Ruzik
Lab Rat
293 posts

If the text = “}” code worke right, but if text = “a}” code doesnt work wright

June 1, 2011

SimonL SimonL
Lab Rat
46 posts

What Type is needNext?
I suspect that you would iterate over that loop 1 to many times it should probably be a< needNext.count(); as count() probably returns the size and you probably require the size -1

June 1, 2011

soroush soroush
Dinosaur Breeder
781 posts

There is an ambiguous ‘else’ here. put contents of every if in {} always. also you don’t need to convert QChar s to QString . they are themselves comparable.
I assume needNext is a QString (is not?)
try tis code:

  1.     for(int a=0; a<=needNext.count(); a++)
  2.     {
  3.         if (needNext[a]!=QChar(' '))
  4.         {
  5.             if (needNext[a]==QChar('}'))
  6.             {
  7.                 qDebug() <<"1"+ needNext[a];
  8.                 isClass=true;
  9.                 // a=needNext.count();
  10.                 // probably you mean:
  11.                 break;
  12.             }
  13.             else
  14.             {
  15.                 //a=needNext.count();
  16.                 break;
  17.                 qDebug() <<"2"+ needNext[a];
  18.             }
  19.         }
  20.     }

If you want to iterate over all characters of a QString, you may want to use a bit more elegant way using iterators:
  1. QString::ConstIterator i = needNext.begin();
  2.     while(i != needNext.end())
  3.     {
  4.          if ( *i !=QChar(' '))
  5.         {
  6.             if ( *i ==QChar('}'))
  7.             {
  8.         // ...
  9.         i++;
  10.     }

 
  ‹‹ Howto implement the QML "Search Box Example" in the Widget world      Can’t insertColumn to an QSqlTableModel ››

You must log in to post a reply. Not a member yet? Register here!