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. IF greater than 90 then minus 90 or if less than 90 add 90
Forum Updated to NodeBB v4.3 + New Features

IF greater than 90 then minus 90 or if less than 90 add 90

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 2.1k 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.
  • B Offline
    B Offline
    BokJr
    wrote on last edited by
    #1

    Here is the code I have so far, Im trying to return an answer in new_axis however is A is less than 90 I need to add 90 and vice versa, if A is more than 90 I need to minus 90, can someone help?

    @ qreal S = ui->sphere->text().toDouble();
    qreal C = ui->cylinder->text().toDouble();
    qreal A = ui->axis->text().toDouble();
    qreal result (C+S);
    qreal answer (A<90then+90orA>90then-90);
    ui->new_sphere->setText(QString::number(result));
    ui->new_cylinder->setText(QString::number(result));
    ui->new_axis->setText(QString::number(answer));@

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

      Hi,

      @
      if ( A < 90) {
      result += 90;
      } else if (A > 90) {
      result -= 90;
      }
      @

      If A == 90 then do nothing, is that right ?

      This question is not really related to Qt, rather basic C/C++. I'd recommend getting a good book about it

      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
      • B Offline
        B Offline
        BokJr
        wrote on last edited by
        #3

        yes thats correct if A=90 then do nothing.

        I am learning basic C/C++ currently, literally started 3 days ago.

        Thank you, I plan to get a book shortly, where I live I have to order this so waiting on it to come. for now you have been a great help.

        So to implement this in my current working, I have the following

        @void Transpose::on_pushButton_clicked()
        {
        qreal S = ui->sphere->text().toDouble();
        qreal C = ui->cylinder->text().toDouble();
        qreal A = ui->axis->text().toDouble();
        qreal result (C+S);
        qreal answer if (A < 90);
        result += 90;
        else if (A > 90);
        result -= 90;
        ui->new_sphere->setText(QString::number(result));
        ui->new_cylinder->setText(QString::number(result));
        ui->new_axis->setText(QString::number(answer));
        }@

        but getting an error, what am I doing wrong?

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

          The code is, take again a look at my example (I should have used another variable name)

          [quote author="BokJr" date="1417082246"]
          @
          qreal answer if (A < 90); << not valid hence the rest also falls apart
          result += 90;
          else if (A > 90);
          result -= 90;
          ui->new_sphere->setText(QString::number(result)); << why not use a QDoubleSpinBox ?
          ui->new_cylinder->setText(QString::number(result)); << same question
          ui->new_axis->setText(QString::number(answer)) << ditto;
          }@
          [/quote]
          Since you are a beginner, don't remove the {} from if/for/while construct even if they are one liner, that will avoid some easy made mistakes.

          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
          • B Offline
            B Offline
            BokJr
            wrote on last edited by
            #5

            The double spin box isn't in use because I want separate answers from S, C and A.

            basically I want it to run as follows that if the user inputs a value into the following

            qreal S = ui->sphere->text().toDouble();
            qreal C = ui->cylinder->text().toDouble();
            qreal A = ui->axis->text().toDouble();
            

            then is runs the calculation c+s=new_sphere,
            new_cylinder= negative value of original C value
            and the A either rotates from 90 to 180 or 180 to 90 (but if the total is below 90 then I need to add 90 and if it's above 90 then I need to subtract 90)

            So if the original S= -2.00
            the original C=+1.00
            and the A=75

            new_sphere= -1.00
            new_cylinder= -1.00
            new_axis= 165 (because its below 90 I added 90 to the total.)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BokJr
              wrote on last edited by
              #6

              Oops that is a newbie mistake, I could use spine boxes :)

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BokJr
                wrote on last edited by
                #7

                So i've come to this

                @void Transpose::on_pushButton_clicked()
                {
                qreal S = ui->sphere->text().toDouble();
                qreal C = ui->cylinder->text().toDouble();
                qreal A = ui->axis->text().toDouble();
                qreal answer (C+S);
                qreal total (-C);
                qreal result;
                if (A <= 90) {
                result +=90;
                } else if (A >= 90) {
                result -=90;
                }
                ui->new_sphere->setText(QString::number(answer));
                ui->new_cylinder->setText(QString::number(total));
                ui->new_axis->setText(QString::number(result));
                }@

                But the new_axis doesn't calculate correctly?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Binary91
                  wrote on last edited by
                  #8

                  In both conditions, you have 'is equal 90' ... (if(less or equal 90) else if(greater or equal 90)), so if 'A' is exactly 90, only the first block will be executed. Maybe you want minus 90 for a value of 90? Then you have to change the condition into:
                  @if(A < 90)
                  result += 90;
                  else if(A >= 90) // or simply else without a condition
                  result -= 90;@

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BokJr
                    wrote on last edited by
                    #9

                    yep I fixed it prior, thanks, the webpage wouldn't let me reply that I had fixed it though.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Binary91
                      wrote on last edited by
                      #10

                      Great! So if your code works, you can change the thread title prepending [SOLVED], so everyone knows it ;-)

                      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