Strange Output : char Variable driving me insane!

 

Hi all,

i'm defining flag variable called char "alertswitch" from 1-10 with their corresponding negative nos. for short positions. I do this  in order to identify certain conditions have been met processing and for documentation purpose. when i ran it inside my function below, it keeps on  generating strange numbers like " 34, 44, 72, 16, -112, 80, 118, -92, 58, -48, 32,..."

even when none of the if conditions are being triggered, alertswitch variable keep on changing! below is the printf result at each steps of the way.

0  Alertswitch1 : 44 : fixalertwitch : 72  alertswitch : 44   (after the function call)

1 Alertswitch1 : 44 : fixalertwitch  : 0 alertswitch : 0 (inside the beginning of the function)

end Alertswitch1 : -34 : fixalertwitch  : -34 alertswitch : -34  (inside the end of the function)

0  Alertswitch1 : -34 : fixalertwitch  : 44  alertswitch : -34  (after the function call)

1 Alertswitch1 : -34 : fixalertwitch  : 0 alertswitch : 0  (inside the beginning of the function)

end Alertswitch1 : -108 : fixalertwitch  : -108 alertswitch : -108    (inside the end of the function)



separately, when i use !=  for alertswitch to control the if-else statment, MQL doesnt respect it at all, even if the alertswitch clearly shows the expression to be true.

Appreciate all your HELP!!!


static char alertswitch;
char fixalertswitch;
static char alertswitch1;
   if (alertswitch !=0) fixalertswitch = alertswitch;


//calling the function
alertswitch = AssessTradeOpp(alertswitch...ClosePrice); 


//this is the function
char AssessTradeOpp ( string &alertswitch1, ...double &ClosePrice)
printf ("out  Alertswitch1 : " + alertswitch)  //

if (A) 
{
        alertswitch = 5; //i set this value for each cases and intends it to be like a 'flag'.   
        doA();
        Check = 1;  //this goes to a Switch statement below the if-else series
}
else if (B
         && alertswitch !=5) //the 5 flag is not being respected and it continues to run the 'then' expression multiple times.
{
        alertswitch = -5; //i set this value for each cases and intends it to be like a 'flag'.   
        doB();
        Check = 2;
}
else if (C)
{
        alertswitch = 20; //i set this value for each cases and intends it to be like a 'flag'.       
        doC();
}
else
{
        alertswitch = -20; //i set this value for each cases and intends it to be like a 'flag'.   
        doD();    
doX();
}

switch(Check){

 case 1:
 case 2:
 default: 
         if (alertswitch==0) 
          
            alertswitch = fixalertswitch;
       
             
     }//end of switch  
      
alertswitch1=alertswitch;
       
printf ("end Alertswitch1 : " + alertswitch1 + " : " + "FixSwitch : " + fixalertswitch + " alertswitch : " + alertswitch  );       
      
return (alertswitch);     

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
JimSingadventure: it keeps on  generating strange numbers like " 34, 44, 72, 16, -112, 80, 118, -92, 58, -48, 32,..."

Your posted code doesn't generate anything. Your switch only functions on zero.

MQL? respects your code.

How To Ask Questions The Smart Way. (2004
          Don't rush to claim that you have found a bug.
Questions Not To Ask
          My program doesn't work. I think system facility X is broken.

It is almost always your code.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the file.
     How To Ask Questions The Smart Way. (2004
          Be precise and informative about your problem

We can't see your broken code.

You do understand that a char can only hold [-128 to 127]
 

thanks william for your pointers...apologies for not including the entire codes here because they are large and spans multi-libraries, i thought it might confuse more... i made some adjustments and now not getting the strange numbers generated but i still do not understand why its happening as it did....let me try out some more and do research on how to make a variable persistent (static doesnt seem to be working) come back when i surrender. 


Thanks again for your kind intent.


William Roeder #:

Your posted code doesn't generate anything. Your switch only functions on zero.

MQL? respects your code.

How To Ask Questions The Smart Way. (2004
          Don't rush to claim that you have found a bug.
Questions Not To Ask
          My program doesn't work. I think system facility X is broken.

It is almost always your code.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the file.
     How To Ask Questions The Smart Way. (2004
          Be precise and informative about your problem

We can't see your broken code.

You do understand that a char can only hold [-128 to 127]
Reason: