building a simple calculator here to get my feet wet with iOS dev. I want to prohibit the entry of more than one decimal point. Put my logic in the comment.
It builds and runs with no problem, but it’s not blocking the entry of more than one “.” any help is appreciated. Thanks.
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleofTypingANumber) {
[display setText:[[display text] stringByAppendingString:digit]];
// if the user enters a decimal point, check if the existing display text contains a "." if not, then allow it, but if it does, do not allow it.
NSString *decimal = @".";
NSRange range = [digit rangeOfString:decimal];
if (range.location == NSNotFound) {
[display setText:digit];
}
} else {
[display setText:digit];
userIsInTheMiddleofTypingANumber = YES;
}
}
I think this should work. It looks like you are appending the digit first thing in your loop, even if a decimal has been entered. I haven’t tried this code in the compiler but it checks to see if a decimal has been entered first.