ios - Calculator divide by zero -


i created simple calculator. works great; however, if divide zero, show error message. know how alert popups, don't know how implement comes when divide zero. here snipped of calculator code:

- (ibaction)buttonoperationpressed:(id)sender {     if (currentoperation == 0) result = currentnumber;     else {         switch (currentoperation) {             case 1:                 result = result + currentnumber;                 break;             case 2:                 result = result - currentnumber;                 break;             case 3:                 result = result * currentnumber;                 break;             case 4:                 result = result / currentnumber;                 break;             case 5:                 currentoperation = 0;                 break;             default:                 break;         }     }     currentnumber = 0;     calcdisplay.text = [nsstring stringwithformat:@"%g",result];     if ([sender tag] == 0) result = 0;     currentoperation = [sender  tag];     userinthemiddleofenteringdecimal = no; 

you have check if second division operand zero, , if yes, print error message. don't forget, can't compare double or ==, have use presicion, this:

case 4:     if(abs(currentnumber) < 1e-12) // 0         // message         return; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -