Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++ "or"
Forum Updated to NodeBB v4.3 + New Features

C++ "or"

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glowdemon1
    wrote on last edited by
    #1

    Heya, I'm looking for something that say a variable is invalid it takes another one instead. In lua we had something like this : time = player.getTime() or 0. If player.getTime() was invalid, it would instead use 0. Is it possible to do this in c++ with not too much code?

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      You can use

      time = "player.getTime() is valid" ? player.getTime() : 0;
      

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glowdemon1
        wrote on last edited by
        #3

        [quote author="Johan Solo" date="1396865569"]You can use

        time = "player.getTime() is valid" ? player.getTime() : 0;
        [/quote]

        Doesn't seem to work, am I doing this correctly?

        @s = QLocale(QLocale::English).toString(totTaxes)? QLocale(QLocale::English).toString(totTaxes) : 0;@

        totTaxes is retrieved from a textEdit, I want s to be 0 if the player hasn't filled anything in the textEdit

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @s = QLocale(QLocale::English).toString(totTaxes) != "" ? QLocale(QLocale::English).toString(totTaxes) : 0;@

          this should work. It compares to an empty string.

          Note: that does call the whole stuff including toString twice. In your case not a problem, but it may be with other calls.

          [edit, koahnig]

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You forgot parentheses:
            @
            s = (QLocale(QLocale::English).toString(totTaxes) != "") ? QLocale(QLocale::English).toString(totTaxes) : 0;
            @
            and it will still not compile becase toString and 0 are different types.

            It's also better to use isEmpty() instead of comparison, because "" will have to be converted to Qstring and then operator= will be called which is heavier than simple emptiness check (probably just reading a counter):
            @
            auto tax = QLocale(QLocale::English).toString(totTaxes);
            s = tax.isEmpty() ? QStringLiteral("0") : tax;
            @

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              Thanks Chris.
              I am not sure, if really need to have the parentheses. But you are certainly correct with the type :(

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • X Offline
                X Offline
                Xander84
                wrote on last edited by
                #7

                I think parentheses are optional with the "?" operator, but c++ is still strongly typed that is why you can't have a string or 0, that is also the reason the "or" operator (||) cannot be used in this case, if you are dealing with values that can be compared to 0 it will work of course, e.g.
                @
                int a = 0;
                int x = a || 42; // of a is 0 then 42 should be used
                @
                also with pointers and stuff, but in general you can only use the "or" with scripting languages (like Lua, PHP or JavaScript), because it implicitly converts many values to "false", that is why you can do that in Lua, but not in c++.

                Chris answer should correct of course, but I am wondering in what case could the string be empty if you convert a number to a QString?? Unless "totTaxes" is not a number, so to the OP can you get as some examples for your "totTaxes" variable and what type it is? :D

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I apologize for the parenthesis comment. It was early, I haven't had my coffee yet and I was thinking of the "common ternary operator pitfall":
                  @
                  if(a == func() ? true : false) //this is what was written
                  if(a == (func() ? true : false)) //this is what was meant
                  if((a == func()) ? true : false) //this is what we got
                  @
                  This of course does not apply in this case as what koahnig wrote is what he meant :)

                  Ints actually do convert automatically to bools in c++ too.
                  This example
                  @int x = a || 42;@
                  actually works like this (though it might not be obvious):
                  @int x = (int)((bool)(a) || (bool)(42)) //because || operates on bools
                  //so it's actually:
                  int x = (int)((bool)(a) || true) //value of a doesn't matter here
                  //which is
                  int x = (int)(true);
                  //which is
                  int x = 1;@
                  This is almost always a typo and what is meant was:
                  @
                  int x = a | 42; //binary "or"
                  //which is 42 for a==0, 43 for a==1 etc.
                  @

                  Getting back to the topic: OP said "totTaxes" is taken from textEdit, which I assume is QTextEdit so it won't actually work with any of our code because it's a QString :)

                  Assuming s is a QString too it should be something like this then:
                  @
                  s = totTaxes.isEmpty() ? QStringLiteral("0") : totTaxes;
                  @
                  without any conversions (except maybe some code to validate the string for some specific format or numerical representation).

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved