Home AMX User Forum NetLinx Studio

yet another array question

here it is


char phone_num[20]
integer count

Define Start
count =1

Define Event
button_event[TP,1]
.
.
.
.
button_event[TP,10]
{
push:
{
if(button.input.channel==10)
phone_num[count]='0'
else
phone_num[count]=itoa(button.input.channel)
count++
}
}

Im trying to save the phone # the users enters in a char array. I just don't understand why even if I set the size of my array to 20, I can't populate individual cells. While debugging array constantly remains empty.

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    First, before I forget it, you could create an array of channel codes and use this for the button events:
    DEFINE_CONSTANT
    INTEGER TeleButtons[] = {1,2,3,4,5,6,7,8,9,10} //digits starting at 1 and ending at 0
    
    DEFINE_VARIABLE
    VOLATILE CHAR phone_num[20]
    VOLATILE CHAR phone_num2[20]
    VOLATILE INTEGER count = 1
    
    DEFINE_EVENT
    button_event[TP,TeleButtons]
    {
    	push:{
    		STACK_VAR INTEGER nBTN;
    		nBTN = GET_LAST(TeleButtons);
    		phone_num[count]=TYPE_CAST(ITOA(nBTN%10));
    		phone_num2[count]=GET_BUFFER_CHAR(ITOA(nBTN%10));
    		SET_LENGTH_ARRAY(phone_num,count);
    		SET_LENGTH_ARRAY(phone_num2,count);
    		IF(count >19)
    			count = 1
    		ELSE
    			count++
    	}
    }
    

    I just tested the above code and it seems to work fine. It is displaying properly in the debug window using Emulate device button pushes. I handled the char conversion in two different ways, choose the one you like best :)

    Jeff
  • jjamesjjames Posts: 2,908
    In your code, count is always being increased. if you want to increase count to be increase only in the ELSE statement, you need to do this:
    button_event[TP,10]
    {
    	push:
    	{
    		if(button.input.channel==10)
    			phone_num[count]='0'
    		else
    		{
    			phone_num[count]=itoa(button.input.channel)
    			count++
    		}
    	}
    }
    
    IF and ELSE statements need brackets if you want more than one thing to happen in the IF/ELSE.
  • Spire_JeffSpire_Jeff Posts: 1,917
    jjames wrote:
    In your code, count is always being increased. if you want to increase count to be increase only in the ELSE statement, you need to do this:
    button_event[TP,10]
    {
    	push:
    	{
    		if(button.input.channel==10)
    			phone_num[count]='0'
    		else
    		{
    			phone_num[count]=itoa(button.input.channel)
    			count++
    		}
    	}
    }
    
    IF and ELSE statements need brackets if you want more than one thing to happen in the IF/ELSE.

    I think the count should increase every time to allow the 0 digit to be entered. The only problem is that count is going to eventually be larger than the array and will just throw INDEX too large at line xxx errors.

    Jeff
  • alexanboalexanbo Posts: 282
    Also note that unless there's more button events stacked on top of the code, it's only ever going to execute the if clause and add a 0 to the first element to the array. When debugging, you may not be able to tell between decimal 0 and ascii zero if the debugger is trying to interpert for you.
  • I do it like this:
    DEFINE_VARIABLE
    
    VOLATILE CHAR VTC_DIAL_STRING[25]
    
    VOLATILE DEVCHAN dcMANUALDIAL[] =
    {
        {dvModeroTP, 120},            //KEYPAD # 0
        {dvModeroTP, 121},            //KEYPAD # 1
        {dvModeroTP, 122},            //KEYPAD # 2
        {dvModeroTP, 123},            //KEYPAD # 3
        {dvModeroTP, 124},            //KEYPAD # 4
        {dvModeroTP, 125},            //KEYPAD # 5
        {dvModeroTP, 126},            //KEYPAD # 6
        {dvModeroTP, 127},            //KEYPAD # 7
        {dvModeroTP, 128},            //KEYPAD # 8
        {dvModeroTP, 129},            //KEYPAD # 9
        {dvModeroTP, 130},            //KEYPAD .
        {dvModeroTP, 131}             //KEYPAD *    
    }
    
    
    DEFINE_EVENT
    
    
    BUTTON_EVENT[dcMANUALDIAL]
    {
        PUSH:
        {
            Stack_Var Integer nIndex;
            Stack_Var Char cDigit[1];
        
            To[Button.Input];
            nIndex = Get_Last(dcMANUALDIAL);
            SELECT
            {
                //  Numbers 0-9
                
                ACTIVE (nIndex <= 10):      cDigit = ITOA(nIndex-1);
                
                //  Period (.) pressed
                
                ACTIVE (nIndex == 11):      cDigit = '.';
                
                //  Star (*) pressed
                
                ACTIVE (nIndex == 12):      cDigit = '*';            
            }
            VTC_DIAL_STRING = "VTC_DIAL_STRING,cDigit";
            SEND_COMMAND Button.Input.Device,"'!T',250,VTC_DIAL_STRING";
        }        
    }
    
    
    BUTTON_EVENT[dvModeroTP,132]                      // CLEAR MANUAL ENTRY
    {
        PUSH:
        {
        }               
        RELEASE:
        {
            VTC_DIAL_STRING = "'";
            SEND_COMMAND Button.Input.Device,"'!T',250,VTC_DIAL_STRING";
        }               
    }
    
    
    BUTTON_EVENT[dvModeroTP,133]                   // BACKSPACE MANUAL ENTRY
    {
        PUSH:
        {
            IF (LENGTH_STRING(VTC_DIAL_STRING))
            {
                TO[BUTTON.INPUT]
                SET_LENGTH_STRING(VTC_DIAL_STRING,LENGTH_STRING(VTC_DIAL_STRING)-1)
                SEND_COMMAND BUTTON.INPUT.DEVICE,"'!T',250,VTC_DIAL_STRING"
            }
        }  
    }
    
    
  • wow thx guys for you help...

    Really appreciate, and so fast as well ..


    :)
  • DHawthorneDHawthorne Posts: 4,584
    Just so you know, your original version didn't work because you were manipulating array elements, but not with any of the functions that tell the compiler the array size has changed. SO it is still seeing it as length zero, therefore "empty." One of the responses used SET_LENGTH_ARRAY, another used SET_LENGH_STRING, and a third used the string concatenation operator (yes, it looks like a lowly comma, but really is much more). All those updated the array size; but changing one element does not. If you watched the variable in a debug window, and set it to display the total length of the variable, you would see your changes, but as soon as you turned that off, it would reset to length zero, and that is what anny reference to the entire array will show as well.
Sign In or Register to comment.