reading keypad

I'm trying to use a keypad to generate midi program change messages. I'm using a variable X to store the number to be transmitted. At the moment, I'm just printing the number on the serial monitor.

pressing A then a number produces 1-10
pressing B then a number produces 11-20
pressing C then a number produces 21-30
pressing D then a number produces 31-40

This works fine. But I want to use the * and # key to increment or decrement the number to be sent and this isn't working.

Any suggestions or alternative methods greatly appreciated.

// read kepad & send a variety of midi messages

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };   // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };   // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Create the Keypad

char modes[6] = "ABCD*#";   // alpha keys to watch for
char mode = "";
int alpha = "";   // has alpha key been pressed

void setup() 
{
 Serial.begin(9600);   // debugging
Serial.flush();
}

void loop() 
{
char key = kpd.getKey();
alpha = 0;
if(key)  // key pressed
  {
  int x= (key - '0');   // convert to suitable integer
   
   for (int m=0; m <= (5); m++) // run through checking match in alphabetic array 
      {
      if (key == modes[m])
          {
          mode = key;  
          alpha = 1;         
          }       
      }   

  if (mode == '*')    // decrement x by 1
               {
                --x;
                Serial.print(" * ");  
                Serial.println(x );   
               }
          if (mode == '#')    // increment x by 1
               {
                ++x;
                Serial.print("#");   
                Serial.println(x );  
               }
      
      if (alpha <1)   // ie it must be numeric
       {
       if (mode == 'A')    
            {
             if (x == 0)
                {x = 10;}  // make 0 into 10
            }            
       if (mode == 'B')    
            {
            x = x+10;
            if (x == 10)
                {x = 20;}  
            }
          if (mode == 'C')    
            {
             x = x+20;
             if (x == 20)
                {x = 30;}
            }          
         if (mode == 'D')   
               {
            x = x+40;
            if (x == 30)
                {x = 40;}
            }

            Serial.print(mode);
            Serial.println(x );             
          } // end numeric entry
  }   // end key pressed
}  // end of void loop
char modes[6] = "ABCD*#";   // alpha keys to watch for
char mode = "";

Seven into six won't go

For your own sanity, and ours, please use the IDE's auto format tool on your code.

7 into 6? I don't understand what you mean.

I wasn't aware of "auto format", will try it

Seven characters, namely 'A', 'B', 'C', 'D', '*', '#' and '\0', won't fit into a six element array.

AWOL:
Seven characters, namely 'A', 'B', 'C', 'D', '*', '#' and '\0', won't fit into a six element array.

The array is called modes, the chr variable is called mode. That part works OK....

Ah, I've solved this. I needed a separate variable to store the number

// read kepad & send a variety of midi messages

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
char keys[ROWS][COLS] = { // Define the Keymap
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };   // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };   // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Create the Keypad

char modes[6] = "ABCD*#";   // alpha keys to watch for
char mode = "";
int alpha = "";   // has alpha key been pressed
int PCnum = 1; // store the program change number

void setup()
{
  Serial.begin(9600);   // debugging
  // Serial.begin(31250);   // midi rate
  Serial.flush();
}

void loop()
{
  char key = kpd.getKey();
  alpha = 0;
  if (key) // key pressed
  {
    int x = (key - '0');  // convert to suitable integer

    for (int m = 0; m <= (5); m++) // run through checking match in alphabetic array
    {
      if (key == modes[m])
      {
        mode = key;
        alpha = 1;
      }
    }

    if (mode == '*')    // decrement x by 1
    {
      --PCnum;
      Serial.print(" * ");
      Serial.println(PCnum );
    }
    if (mode == '#')    // increment x by 1
    {
      ++PCnum;
      Serial.print("#");
      Serial.println(PCnum );
    }

    if (alpha < 1)  // ie it must be numeric
    {
      if (mode == 'A')
      {
        if (x == 0)
        {
          x = 10; // make 0 into 10
        }
      }
      if (mode == 'B')
      {
        x = x + 10;
        if (x == 10)
        {
          x = 20;
        }
      }
      if (mode == 'C')
      {
        x = x + 20;
        if (x == 20)
        {
          x = 30;
        }
      }
      if (mode == 'D')
      {
        x = x + 40;
        if (x == 30)
        {
          x = 40;
        }
      }
      PCnum = x;
      Serial.print(mode);
      Serial.println(PCnum );
    } // end numeric entry
  }   // end key pressed
}  // end of void loop
foobar.ino:17:17: warning: initializer-string for array of chars is too long [-fpermissive]
 char modes[6] = "ABCD*#";   // alpha keys to watch for
                 ^
foobar.ino:18:13: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
 char mode = "";
             ^
foobar.ino:19:13: warning: invalid conversion from 'const char*' to 'int' [-fpermissive]
 int alpha = "";   // has alpha key been pressed
             ^

Like I said, seven into six doesn't go.