Nano HE Nano HE - 3 years ago 85
C++ Question

How can I assign the positive value?

I declared an

int
variable like this,

int temp1 = -3;


How can I assign the int value
3
to
temp2
?

I can only find the method as this,

If (temp1 < 0){
temp2 = 0 - temp1;
}


Is there a good method to approach this?

Answer Source

Try using abs:

temp2 = abs(temp1);

ideone

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download