Colon sign with case and enum

Hello. I tried to find an answer and all of them in the archive, but they didn't explain my example. Or maybe I didn't get it.

1
2
3
4
5
6
7
8
int calc_mode=(int)SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE);
   string str_calc_mode;
   switch(calc_mode)
     {
      case SYMBOL_CALC_MODE_FOREX:str_calc_mode="Forex";break;
      case SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE:str_calc_mode="Forex No Leverage";break;
      case SYMBOL_CALC_MODE_FUTURES:str_calc_mode="Futures";break;
     }


str_calc_mode returns text, e.g. "Forex", "Futures".

The second parameter in SymbolInfoInteger function SYMBOL_TRADE_CALC_MODE is enum (SYMBOL_CALC_MODE_FOREX, SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE, SYMBOL_CALC_MODE_FUTURES).

What I can not understand - how it returns text (string), if calc_mode=(int)SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE) returns int (a number)?

And what does colon sign means in this code? What is it for?
calc_mode is an integer. It stores the integer value returned by SymbolInfoInteger().

str_calc_mode is a string.

The switch-case block is comparing the number stored in calc_mode (i.e. the number returned by SymbolInfoInteger()), and comparing it to the numerical values defined in the enum. It then sets the contents of str_calc_mode to an appropriate value.

E.g. if calc_mode has the value SYMBOL_CALC_MODE_FOREX, the following code executes:

str_calc_mode="Forex";break;

What I can not understand - how it returns text

How what returns text? Be specific, and be precise. We can't read your mind.

And what does colon sign means in this code? What is it for?

It's just the usual syntax for a switch-case statement. It's not doing anything unusual here. Technically, it's used to denote a label, but that's just how switch-case statements work in C/C++, by using labels.
Last edited on
Hello alexko,

Line 1 appears to be calling a function to initialize "calc_mode" and you are typecasting the return value to an "int" to store it in "casc_mode", but you need to show the "SymbolInfoInteger" function so everyone can see what it is doing.

You also mention an "enum", but failed to show what it looks like.

In the switch:
1
2
3
4
5
6
7
	switch (switch_on)
	{
		case 1:
			break;
		default:
			break;
	}

The "switch_on" is any type of integral type, usually an "int", or a "char" type.

The (:) marks the end of the "case" label and whatever follows up to the "break: is part of the case.

http://www.cplusplus.com/doc/tutorial/control/ The last section on the page.

Andy
Wow, thank you, guys! I didn't expect for so quick reply and sorry if I was not specific.

To MikeyBoy

Thank you, Mike. I knew that "calc_mode is an integer" "returned by SymbolInfoInteger()". What I don't understand is step-by-step execution.

From your words: "The switch-case block is comparing the number stored in calc_mode (i.e. the number returned by SymbolInfoInteger()), and comparing it to the numerical values defined in the enum. It then sets the contents of str_calc_mode to an appropriate value."
E.g. if calc_mode has the value SYMBOL_CALC_MODE_FOREX, the following code executes:
str_calc_mode="Forex";break;"

You see, I'm missing the point where int becomes string :) e.g. how "calc_mode=0" can be one of the strings from enum?

Step-by-step:
1) calc_mode=0;
2) what then switch(calc_mode) do? how switch->case knows string from "0" (zero)?

sorry if my questions are stupid :) I asked it here, because any example is better than long-read of specification, which I also read ((
Enum labels are not strings, they are aliases for numbers. Constant numbers, but still numbers.

https://www.learncpp.com/cpp-tutorial/45-enumerated-types/

Creating enum classes makes things a bit less numeric.

https://www.learncpp.com/cpp-tutorial/4-5a-enum-classes/
thank you - the tutorial is very detailed and interesting.

Did I understand it right?

enum SYMBOL_TRADE_CALC_MODE consists of labels: SYMBOL_CALC_MODE_FOREX, SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE, SYMBOL_CALC_MODE_FUTURES.

so when a case check for SYMBOL_CALC_MODE_FOREX, the case does not check for a number, it checks for a label's name, right?

And the function SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE) returns a number with an alias, right?

that's why a case can compare text to an alias, right?

So, from my example steps are:
1) calc_mode returns "0" with the alias "SYMBOL_CALC_MODE_FOREX"
2) case checks whether "SYMBOL_CALC_MODE_FOREX"=="SYMBOL_CALC_MODE_FOREX"
3) then if true returns "forex"
4) break or next case
Last edited on
Topic archived. No new replies allowed.