Skip to main content
edited the code
Source Link
Dave X
  • 2.4k
  • 16
  • 30
  1. make buffer[] and i static.

  2. get rid of the while() loop.

Like this:

int getTheNumber()
{
    static char buffer[4];
    static int i=0;
    // Input up to 3 numbers until we find a * or #
    char key = keypad.getKey();

    // If it's a number AND we have space left, add to our string
    if ('0' <= key && key <= '9' && i < 3)
        {
            buffer[i] = key;
            i++; 
            return(-1)       
        }
        // If it's a * or #, end
    else if ('#' == key && i > 0)
        {
            // Null terminate
            buffer[i] =0; 
            int value = atoi(buffer);
            i=0;
            return atoi(buffer);
        }        
}
  1. make buffer[] and i static.

  2. get rid of the while() loop.

  1. make buffer[] and i static.

  2. get rid of the while() loop.

Like this:

int getTheNumber()
{
    static char buffer[4];
    static int i=0;
    // Input up to 3 numbers until we find a * or #
    char key = keypad.getKey();

    // If it's a number AND we have space left, add to our string
    if ('0' <= key && key <= '9' && i < 3)
        {
            buffer[i] = key;
            i++; 
            return(-1)       
        }
        // If it's a * or #, end
    else if ('#' == key && i > 0)
        {
            // Null terminate
            buffer[i] =0; 
            int value = atoi(buffer);
            i=0;
            return atoi(buffer);
        }        
}
Source Link
dannyf
  • 2.8k
  • 11
  • 13

  1. make buffer[] and i static.

  2. get rid of the while() loop.