iOS Debugger Crashes When Inputting 0.0 In Textfield

I am currently working on an iOS app in Xcode, and I have came up with code to count the number of significant digits. It is written in C++, but I did some change to get it to work. Whenever I input a zero value, It crashes, bu anything else works perfectly fine.

My code is as follows:

- (IBAction)sigFigCount:(UITextField *)thetextfield
{
length = 0;
if (thetextfield == _textfield1)
{
    if ([thetextfield.text length] > 0)//If TextField Has More Than 0 digits...
    {
        text1 = std::string([_textfield1.text UTF8String]);
        while (text1.at(0) == '0' || text1.at(0) == '.')//Trim Leading Zeros...
        {
            text1 = text1.substr(1);
        }
        length = text1.length();
        decimal = text1.find('.');
        if (decimal >= 0 && decimal < text1.length())//Dont count decimal as sig fig...
        {
            length -= 1;
        }
        if ([[_textfield1 text] doubleValue] == 0.0)
        {
            NSLog(@"HERE");
            self.display3.text = @"1";
        }
        NSString *siggy = [NSString stringWithFormat:@"%i", length];
        self.display3.text = siggy;
    }
    if ([thetextfield.text length] == 0)
    {
        length = 0;
        NSString *ifzero = [NSString stringWithFormat:@"%i", length];
        self.display3.text = ifzero;
    }
    if ([[thetextfield text] doubleValue] == 0.0)
    {
        newLength = 1;
        NSString *zeroVal = [NSString stringWithFormat:@"%i", newLength];
        self.display3.text = zeroVal;
    }
    NSString *norm = [NSString stringWithFormat:@"%i", length];
    self.display3.text = norm;
}
}

Please Help, I believe it has something to do with the way numbers are represented in memory... but the NSLog worked when I put it in a while statement... Any input is appreciated.

Thank You

Answers


while (text1.at(0) == '0' || text1.at(0) == '.')//Trim Leading Zeros...
        {
            text1 = text1.substr(1);
        }

I believe this is where your problem lies. Your test string '0.0' contains only 0 and . characters. After 3 times through the loop, text1 is the empty string, yet you're still trying to access the first character.

This line also probably doesn't do what you expect: if ([[thetextfield text] doubleValue] == 0.0)

Anything that's not a number will get converted to 0.0, so [@"foo" doubleValue] == 0.0 is true as well.

There are quite a few other problems here too like the use of UTF8String. If the user typed anything other than low ascii characters, strange things will happen.

You really shouldn't need any C++ here. This would be very easy with objective C only.

A couple other comments... You probably want if/else if/else if instead of just 3 consecutive elses.

The last if could also be easily converted from:

   if ([[thetextfield text] doubleValue] == 0.0)
    {
        newLength = 1;
        NSString *zeroVal = [NSString stringWithFormat:@"%i", newLength];
        self.display3.text = zeroVal;
    }

to

if (...) {
  self.display3.text = @"1";
}

Need Your Help

php mysql check existing syntax

php mysql syntax

I have an ordering system from 1 to 6 (both inclusive), which the order number is assigned to a news in table featured by user's choice. When a user saves an order for a news, in the table featured...

fixed div went out of the wrapper

css html position fixed

I have this side menu that I wanted to be fixed so it will follow the user when the user scrolls. I'm planning to put jumping targets inside it because the contents are very long.