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



  • 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));@


  • Lifetime Qt Champion

    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



  • 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?


  • Lifetime Qt Champion

    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.



  • 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.)



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



  • 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?



  • 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;@



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.