Creating Array using IF

 

I am trying to create a string array based on an ENUM that I created for a drop down list. The enum and drop down list work fine.  When I create an Array based on the enum selected that's when I run into problems.

enum ENUM_BASKET {
      USD=0,    //USD
      EUR=1,    //EUR
      GBP=2,    //GBP
      AUD=3,    //AUD
      CAD=4,    //CAD
      NZD=5,    //NZD
      CHF=6,    //CHF
      JPY=7    //JPY
      };
extern ENUM_BASKET Currency=USD;
if (Currency==USD){string pair[]={"EURUSD","GBPUSD","AUDUSD","NZDUSD","USDCAD","USDCHF","USDJPY"};
if (Currency==EUR){string pair[]={"EURUSD","EURGBP","EURAUD","EURNZD","EURCAD","EURCHF","EURJPY"};
if (Currency==GBP){string pair[]={"GBPUSD","EURGBP","GBPAUD","GBPNZD","GBPCAD","GBPCHF","GBPJPY"};
if (Currency==JPY){string pair[]={"USDJPY","GBPJPY","AUDJPY","NZDJPY","CADJPY","CHFJPY","EURJPY"};
if (Currency==CAD){string pair[]={"EURCAD","GBPCAD","AUDCAD","NZDCAD","USDCAD","CADCHF","CADJPY"};
if (Currency==AUD){string pair[]={"EURAUD","GBPAUD","AUDUSD","AUDNZD","AUDCAD","AUDCHF","AUDJPY"};
if (Currency==NZD){string pair[]={"EURNZD","GBPNZD","NZDUSD","AUDNZD","NZDCAD","NZDCHF","NZDJPY"};
if (Currency==CHF){string pair[]={"USDCHF","GBPCHF","AUDCHF","NZDCHF","CADCHF","CHFJPY","EURCHF"};

Whenever I compile it gives a warning saying variable pair is not used and then later an error saying pair is not declared. I have also tried changing "Currency==USD" to ==0, and used EnumtoString and even IntegerToString. Any help on this would be fantastic. I'm sure it's something very basic I'm over looking.

 

Maybe because pair[] exists only within the brackets as it was declared there.

You have to declare once and outside the if..

BTW , is there a final } is missing at each line? No compiler error about this?

 
Carl Schreiber:

Maybe because pair[] exists only within the brackets as it was declared there.

You have to declare once and outside the if..

BTW , is there a final } is missing at each line? No compiler error about this?

thank you, i made a mistake with posting the code, there is no { before string, so no error there.


I did declare the pair[] outside the if statement in the global variables section, with  string pair[]; it replaced the error with repeated warnings : declaration of 'pair' hides global declaration at line XX and still continues to give the 1st warning of variable 'pair' not used.

 
Carl Schreiber:

Maybe because pair[] exists only within the brackets as it was declared there.

You have to declare once and outside the if..

BTW , is there a final } is missing at each line? No compiler error about this?

That will not work as you can't initialize an array this way outside the declaration.
 
theone964:

I am trying to create a string array based on an ENUM that I created for a drop down list. The enum and drop down list work fine.  When I create an Array based on the enum selected that's when I run into problems.

Whenever I compile it gives a warning saying variable pair is not used and then later an error saying pair is not declared. I have also tried changing "Currency==USD" to ==0, and used EnumtoString and even IntegerToString. Any help on this would be fantastic. I'm sure it's something very basic I'm over looking.

Because you need to remove the declarations ("string ...") inside your brackets. But anyway it will give other errors. I would suggest you to use a Two-dimensional array.

input ENUM_BASKET Currency=USD;
string pair[][7]=
  {
     {"EURUSD","GBPUSD","AUDUSD","NZDUSD","USDCAD","USDCHF","USDJPY"},
     {"EURUSD","EURGBP","EURAUD","EURNZD","EURCAD","EURCHF","EURJPY"},
     {"GBPUSD","EURGBP","GBPAUD","GBPNZD","GBPCAD","GBPCHF","GBPJPY"},
     {"USDJPY","GBPJPY","AUDJPY","NZDJPY","CADJPY","CHFJPY","EURJPY"},
     {"EURCAD","GBPCAD","AUDCAD","NZDCAD","USDCAD","CADCHF","CADJPY"},
     {"EURAUD","GBPAUD","AUDUSD","AUDNZD","AUDCAD","AUDCHF","AUDJPY"},
     {"EURNZD","GBPNZD","NZDUSD","AUDNZD","NZDCAD","NZDCHF","NZDJPY"},
     {"USDCHF","GBPCHF","AUDCHF","NZDCHF","CADCHF","CHFJPY","EURCHF"}
  };
...
   //--- Use pair[Currency][0..6]
   
 
Alain Verleyen:

Because you need to remove the declarations ("string ...") inside your brackets. But anyway it will give other errors. I would suggest you to use a Two-dimensional array.

This worked perfectly. Thank you so very much.
Reason: