Results 1 to 6 of 6

Thread: Not able to recognize "-"

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Not able to recognize "-"

    Hi,

    I am using Qt 3 and trying a parse a string entered by the user. For this i am extracting each character and playing with it. The problem is when i try to compare a QString which is contains "-" with "-" the result is false.

    Has someone encountered this kind of problem ? Am I doing something wrong? Here is the code:

    Qt Code:
    1. for(pos=0;pos<expr.length();pos++)
    2. {
    3. QString curr = expr.at(pos);
    4. if(curr == ":")
    5. {
    6. OperatorParse("+");
    7. }
    8. else if((curr == QString(QChar(0xD7)))||(curr==QString(QChar(0xDD)))||
    9. (curr == "+")||(curr == "-")||(curr == "*")||(curr == "/")||(curr == "^"))
    10. {
    11. OperatorParse(curr);
    12. }
    13. else if(curr == "(")
    14. {
    15. LeftParse(0, pos);
    16. }
    17. else if(curr == "{")
    18. {
    19. LeftParse(1, pos);
    20. }
    21. else if(curr == "[")
    22. {
    23. LeftParse(2, pos);
    24. }
    25. else if((curr == ")")||(curr == "}")||(curr == "]"))
    26. {
    27. RightParse();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    expr is the string that i am parsing.
    I entered "2 - 2" but the - is not recongnized

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Not able to recognize "-"

    I guess this would be more efficient:
    Qt Code:
    1. for(pos=0;pos<expr.length();pos++){
    2. const char curr = expr[pos];
    3. switch(curr){
    4. case ':':
    5. OperatorParse('+');break;
    6. case '+':
    7. case '-':
    8. case '*':
    9. case '/':
    10. case '^':
    11. OperatorParse(curr); break;
    12. case '(':
    13. LeftParse(0, pos); break;
    14. case '{':
    15. LeftParse(1, pos); break;
    16. case '[':
    17. LeftParse(2, pos); break;
    18. case ')':
    19. case '}':
    20. case ']':
    21. RightParse(); break;
    22. default:
    23. std::cerr << "Ignored: " << curr << " (" << (int)curr << ")" << std::endl;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    This way you'll know exactly what happens at each moment.

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: Not able to recognize "-"

    I guess this would be more efficient
    Thanks for the idea, but can I use

    QString(QChar(0xD7)) and (curr==QString(QChar(0xDD)) with switch case ?

    Also the I have noticed that there are more than one character that is similar to "-". The user was actually not typing minus but some other similar character.

    Thanks a lot

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Not able to recognize "-"

    Quote Originally Posted by munna
    Thanks for the idea, but can I use

    QString(QChar(0xD7)) and (curr==QString(QChar(0xDD)) with switch case ?
    Qt Code:
    1. case 0xd7: case 0xdd: ...
    To copy to clipboard, switch view to plain text mode 

    You can also use QChar::unicode() and use unicode values in switch cases.

    Also the I have noticed that there are more than one character that is similar to "-". The user was actually not typing minus but some other similar character
    Probably &ndash; or &mdash; These are available as unicode characters, you should either disallow them or convert them to a minus before parsing. The alphabet should be as simple as possible.

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: Not able to recognize "-"

    Qt Code:
    1. const char curr = expr[pos];
    To copy to clipboard, switch view to plain text mode 

    gives me the following error

    error C2666: 'QString:perator`[]'' : 3 overloads have similar conversions
    When I use

    Qt Code:
    1. const char curr = expr.constref(pos);
    To copy to clipboard, switch view to plain text mode 

    The case 0xdd and 0xd7 doesn't work.

    Thanks a lot

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Not able to recognize "-"

    So use QChar instead of const char And make a conversion to some int type in the switch so you can make case labels. You have latin1 and unicode conversions available.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.