StringConcatenate does not preserve the string_var in for loop

To add comments, please log in or register
samjesse
662
samjesse  

Hi

Please see the comment in the code below,  the Print(dt) in the second time suppose to print the date and time, but it only prints the time even though the dt variable has not be re initialized. Why the dt variable did not keep its value between the loops when it is out side the loop?

Thanks

        
     for(int i=0; i<5; i++)
        {
        string a, dt, b;

        for(int j=0; j<44); j++)
          {
          CEasyXmlNode *SubNode=ChildNode.Children().At(j);
          string name = SubNode.getName();
          string value = SubNode.getValue();

          string toWrite2 = IntegerToString(i)+"|"+IntegerToString(j)+"|"+name+"|"+value;
          FileWrite(file_handle, toWrite2);
            if (name == "date"){
              string res[3];
              StringSplit(value, StringGetCharacter("-",0), res);
              StringConcatenate(dt, res[2], ".", res[0], ".", res[1], " ");

              Print(dt); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2018.08.22

            } else if (name == "time"){
              string tm[2];
              StringSplit(value, StringGetCharacter("-",0), tm);
              StringConcatenate(dt, tm[0], ":", tm[1]);

              Print(dt); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1:30  but expected is 2018.08.22 1:30

            } 
         }
      }
Keith Watford
Moderator
14550
Keith Watford  
Where do you initialize dt? Its value will be an empty string.
samjesse
662
samjesse  
I changed the line
string a, dt, b;

to

string a, b;
string dt = "";


so that it gets initialized, but for no avail. Still the same problem.
Keith Watford
Moderator
14550
Keith Watford  
samjesse:
I changed the line

to


so that it gets initialized, but for no avail. Still the same problem.

What do you expect "" to print?

It's still an empty string.

samjesse
662
samjesse  
Keith Watford:

What do you expect "" to print?

It's still an empty string.

and that is what
StringConcatenate

suppose to change before printing. What am I missing????

Minions Labs
1719
Minions Labs  

Your code is a mess.

Stop declaring variables as you go, it makes the code unreadable. All variablers are declared at the top of the Function/Method/Class/Whatever.

And who (and where) the hell are "a" and "b"?  Also, there is no memory cost on naming your variables in a meaningful way.

The code is running as expected. Do you know what an ELSE IF does??

Keith Watford
Moderator
14550
Keith Watford  

samjesse:
and that is what

StringConcatenate

suppose to change before printing. What am I missing????

Sorry, I was thinking of mql4, not mql5

samjesse
662
samjesse  
Minions Labs:

Your code is a mess.

Stop declaring variables as you go, it makes the code unreadable. All variablers are declared at the top of the Function/Method/Class/Whatever.

And who (and where) the hell are "a" and "b"?  Also, there is no memory cost on naming your variables in a meaningful way.

The code is running as expected. Do you know what an ELSE IF does??

The reason for having the variable inside the first loop (and not the the beginning of the function) is to clear it from it content every time so that the second loop builds the new intended string consisting of the date and time "as coded".

ELSE IF block will be executed the first time which will allow the "dt" string to have the date and the second time to add the time to the date. Remember, the "dt" has not be re initialized to a "" string yet as the second loop is running. It only gets re initialized when the first loop runs.

What Am I missing???

samjesse
662
samjesse  

It appears to me that when the else if block is exited, the work of the StringConcatenate is lost, which is counter intuitive. notice the signature from the docs. the first variable is passed by reference.


int  StringConcatenate(
   string&  string_var,   // string to form
   void argument1         // first parameter of any simple type
   void argument2         // second parameter of any simple type
   ...                    // next parameter of any simple type
   );
Andrey Barinov
42402
Andrey Barinov  
samjesse:

It appears to me that when the else if block is exited, the work of the StringConcatenate is lost, which is counter intuitive. notice the signature from the docs. the first variable is passed by reference.


Are you running this code in MT4 or MT5? Because there is difference :)

samjesse
662
samjesse  
Andrey Barinov:

Are you running this code in MT4 or MT5? Because there is difference :)

MT5.

12
To add comments, please log in or register