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. problems on using the if and else statement and updating the data.
Forum Updated to NodeBB v4.3 + New Features

problems on using the if and else statement and updating the data.

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++ qtupdateif and else
18 Posts 8 Posters 3.6k Views 4 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.
  • M Mr.Nobody645

    Right now Im new to this programming and Im having the problems of using the if and else statement on (data && data). Next is the update part which i really dont get it. Saw a lot of videos of using MySQL instead of normal programming update. So here is my code

    f (ui-> AddOn1 -> isChecked()){
            price = 2.50;
        }
        else if (ui-> AddOn2 -> isChecked()){
            price = 1.20;
        }
        else if (ui-> AddOn3 -> isChecked()){
            price = 1.10;
        }
        else if (ui-> (AddOn1 + AddOn2 )-> isChecked()){
            price = 3.70;
        }
        else if (ui-> (AddOn1 + AddOn3 )-> isChecked()){
            price = 3.60;
        }
        else if (ui-> (AddOn2 + AddOn3) -> isChecked()){
            price = 2.30;
        }
        else if (ui-> (AddOn1 + AddOn2 + AddOn3) -> isChecked()){
            price = 4.80;
        }
    
    
    
    
        total = price;
        total = ui -> lbTotal ->text().toDouble()+price;
        ui ->lbTotal->setText(QString::number(total,'f',2));
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @Mr-Nobody645
    if ... else is just C++.

    In principle your code approach looks about what is necessary.

    But what in the world is

    else if (ui-> (AddOn1 + AddOn2 )-> isChecked()){
    

    supposed to be?? Is it maybe:

    else if (ui-> AddOn1-> isChecked() && ui-> AddOn2-> isChecked()){
    

    ? In which case you'll need to test that before testing ui-> AddOn1-> isChecked() or ui-> AddOn2-> isChecked() individually.

    You should read up on basic C++/programming. It's not to do with Qt, and you won't be able to do anything effectively in Qt (or any other toolkit) if you have simple programming issues. I realize this is your first post, but very kindly/helpful advice you should know that programming issues are better dealt with as such than on a Qt-specific forum.

    M 1 Reply Last reply
    3
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #3

      Do you actually want to ask some question here? What is your problem?

      Do you mean to ask how to check multiple values at once in an if statement?

      ui-> (AddOn1 + AddOn2 + AddOn3) -> isChecked()

      Here is something which should work:

      if (ui->AddOn1->isChecked() && ui->AddOn2->isChecked() && ui->AddOn3->isChecked())
      

      But for sake of readability and maintenance (and, to some degree, performance), I'd suggest to get state first, and check later:

      const bool isAddOn1 = ui->AddOn1->isChecked();
      const bool isAddOn2 = ui->AddOn2->isChecked();
      const bool isAddOn3 = ui->AddOn3->isChecked();
      if (isAddOn1 && isAddOn2 && isAddOn3)
      // ...
      

      All modernt compilers also understand and as a synonym to && so you can do:

      if (isAddOn1 and isAddOn2 and isAddOn3)
      

      (Z(:^

      JonBJ 1 Reply Last reply
      1
      • M Offline
        M Offline
        Mr.Nobody645
        wrote on last edited by
        #4

        omg ty for the if and else part. Really appreciate your help.

        1 Reply Last reply
        0
        • JonBJ JonB

          @Mr-Nobody645
          if ... else is just C++.

          In principle your code approach looks about what is necessary.

          But what in the world is

          else if (ui-> (AddOn1 + AddOn2 )-> isChecked()){
          

          supposed to be?? Is it maybe:

          else if (ui-> AddOn1-> isChecked() && ui-> AddOn2-> isChecked()){
          

          ? In which case you'll need to test that before testing ui-> AddOn1-> isChecked() or ui-> AddOn2-> isChecked() individually.

          You should read up on basic C++/programming. It's not to do with Qt, and you won't be able to do anything effectively in Qt (or any other toolkit) if you have simple programming issues. I realize this is your first post, but very kindly/helpful advice you should know that programming issues are better dealt with as such than on a Qt-specific forum.

          M Offline
          M Offline
          Mr.Nobody645
          wrote on last edited by
          #5

          im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.

          J.HilkJ JonBJ 2 Replies Last reply
          0
          • M Mr.Nobody645

            im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @Mr-Nobody645 said in problems on using the if and else statement and updating the data.:

            im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.

            obviously, as

            if (ui-> AddOn1 -> isChecked()){
            price = 2.50;
            }

            comes before everything else and its true so the rest of the checks is not executed


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • M Mr.Nobody645

              im appreciate for both of you on helping me in the if and else part. However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Mr-Nobody645 said in problems on using the if and else statement and updating the data.:

              However, when i try to checked AddOn1 and AddOn2, i got my price 2.50 instead of 3.70.

              That's why I wrote earlier:

              In which case you'll need to test that [if (ui-> AddOn1-> isChecked() && ui-> AddOn2-> isChecked())] before testing ui-> AddOn1-> isChecked() or ui-> AddOn2-> isChecked() individually.

              1 Reply Last reply
              0
              • sierdzioS sierdzio

                Do you actually want to ask some question here? What is your problem?

                Do you mean to ask how to check multiple values at once in an if statement?

                ui-> (AddOn1 + AddOn2 + AddOn3) -> isChecked()

                Here is something which should work:

                if (ui->AddOn1->isChecked() && ui->AddOn2->isChecked() && ui->AddOn3->isChecked())
                

                But for sake of readability and maintenance (and, to some degree, performance), I'd suggest to get state first, and check later:

                const bool isAddOn1 = ui->AddOn1->isChecked();
                const bool isAddOn2 = ui->AddOn2->isChecked();
                const bool isAddOn3 = ui->AddOn3->isChecked();
                if (isAddOn1 && isAddOn2 && isAddOn3)
                // ...
                

                All modernt compilers also understand and as a synonym to && so you can do:

                if (isAddOn1 and isAddOn2 and isAddOn3)
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #8

                @sierdzio said in problems on using the if and else statement and updating the data.:

                All modernt compilers also understand and as a synonym to && so you can do:

                I am speechless! Apart from the fact that I have never heard of this (old time C/C++ programmer, now returned to new C++). And apart from the fact that introducing and as a language keyword would potentially break one million old C/C++ programs which could have used and as a variable?

                You are obviously more expert than I, but do you want to recommend this to a newbie? I have never noticed and used in a C++ program, if OP is to produce standard code, and recognize others' code, would you not recommend sticking with &&?

                J.HilkJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @sierdzio said in problems on using the if and else statement and updating the data.:

                  All modernt compilers also understand and as a synonym to && so you can do:

                  I am speechless! Apart from the fact that I have never heard of this (old time C/C++ programmer, now returned to new C++). And apart from the fact that introducing and as a language keyword would potentially break one million old C/C++ programs which could have used and as a variable?

                  You are obviously more expert than I, but do you want to recommend this to a newbie? I have never noticed and used in a C++ program, if OP is to produce standard code, and recognize others' code, would you not recommend sticking with &&?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @JonB apparently part of the standard since c++98
                  https://en.cppreference.com/w/cpp/keyword/and


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  2
                  • J.HilkJ J.Hilk

                    @JonB apparently part of the standard since c++98
                    https://en.cppreference.com/w/cpp/keyword/and

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #10

                    @J-Hilk , @sierdzio
                    Absolutely shocking! I see from https://en.cppreference.com/w/cpp/language/operator_alternative that there are actually 11 new reserved words for operators. Including not, and even the string compl, which I might have used as variables and would break upgrading to C++ '98.

                    Thinking aloud, I believe all C operators were symbols and not words, part of the charm (for right or for wrong) of the language.

                    With all due respect to @sierdzio's comment, which I accept & respect, I have still never seen a piece of C++ code using any of these.... [And hence would caution a beginner against adopting these.]

                    [C++ looks like it's trying to be a Python-wannabe ;-) ]

                    jsulmJ 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @J-Hilk , @sierdzio
                      Absolutely shocking! I see from https://en.cppreference.com/w/cpp/language/operator_alternative that there are actually 11 new reserved words for operators. Including not, and even the string compl, which I might have used as variables and would break upgrading to C++ '98.

                      Thinking aloud, I believe all C operators were symbols and not words, part of the charm (for right or for wrong) of the language.

                      With all due respect to @sierdzio's comment, which I accept & respect, I have still never seen a piece of C++ code using any of these.... [And hence would caution a beginner against adopting these.]

                      [C++ looks like it's trying to be a Python-wannabe ;-) ]

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @JonB said in problems on using the if and else statement and updating the data.:

                      I have still never seen a piece of C++ code using any of these

                      Me neither :-)

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #12

                        I have seen it but very, very rarely.

                        And hence would caution a beginner against adopting these

                        I'm split on this :D One one hand, && is clearly, vastly, totally the established and well-known convention practically everywhere. So using it is definitely a good idea.

                        But on the other hand, and is more readable and convenient. So it is also a good idea.

                        (Z(:^

                        JonBJ 1 Reply Last reply
                        1
                        • sierdzioS sierdzio

                          I have seen it but very, very rarely.

                          And hence would caution a beginner against adopting these

                          I'm split on this :D One one hand, && is clearly, vastly, totally the established and well-known convention practically everywhere. So using it is definitely a good idea.

                          But on the other hand, and is more readable and convenient. So it is also a good idea.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #13

                          @sierdzio said in problems on using the if and else statement and updating the data.:

                          But on the other hand, and is more readable and convenient.

                          In Pascal or Python, yes. When scanning C/C++ I expect to see all operators as symbols (and not some but not others) Next C++ will be offering begin/end for {/}! [Some people used to do all this words-for-symbols via C #defines.] I give up on the modern world, it's all going to pot... ;-)

                          J.HilkJ 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @sierdzio said in problems on using the if and else statement and updating the data.:

                            But on the other hand, and is more readable and convenient.

                            In Pascal or Python, yes. When scanning C/C++ I expect to see all operators as symbols (and not some but not others) Next C++ will be offering begin/end for {/}! [Some people used to do all this words-for-symbols via C #defines.] I give up on the modern world, it's all going to pot... ;-)

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #14

                            @JonB said in problems on using the if and else statement and updating the data.:

                            Next C++ will be offering begin/end for {/}!

                            you can use

                            {	??<
                            }	??>
                            [	??(
                            ]	??)
                            #	??=
                            \	??/
                            ^	??'
                            |	??!
                            ~	??-
                            

                            but that was removed in c++17, :)


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            JonBJ 1 Reply Last reply
                            1
                            • J.HilkJ J.Hilk

                              @JonB said in problems on using the if and else statement and updating the data.:

                              Next C++ will be offering begin/end for {/}!

                              you can use

                              {	??<
                              }	??>
                              [	??(
                              ]	??)
                              #	??=
                              \	??/
                              ^	??'
                              |	??!
                              ~	??-
                              

                              but that was removed in c++17, :)

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #15

                              @J-Hilk

                              but that was removed in c++17, :)

                              Even better --- you start using new language constructs and then they remove them so that your code gets broken!

                              FWIW, I note from https://en.cppreference.com/w/cpp/language/operator_alternative

                              There are alternative spellings for several operators and other tokens that use non-ISO646 characters.

                              So the motivation never was "clarity", it's to do with people having funny keyboards....

                              1 Reply Last reply
                              1
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @JonB said in problems on using the if and else statement and updating the data.:

                                funny keyboards

                                The hardware is innocent ;-P

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • Kent-DorfmanK Offline
                                  Kent-DorfmanK Offline
                                  Kent-Dorfman
                                  wrote on last edited by
                                  #17

                                  whats next, espousing the virtues of trigraphs?

                                  1 Reply Last reply
                                  0
                                  • 1 Offline
                                    1 Offline
                                    1Manui1
                                    wrote on last edited by
                                    #18

                                    How did you do it now?

                                    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