For example:
switch (CStringInputMonth)
{case "JAN":
intMonth = 1;
break;
case "Feb"
intMonth =2;
break;
....
};
VC++ 6.0 will not accept the CString in the switch, and says that "JAN" is
not a constant value.
Any suggestions will be appreciated
TIA
Frank
"David J. Zook" <dave...@my-deja.com> wrote in message
news:OVl0MZui$GA....@cppssbbsa02.microsoft.com...
I will have to get used to "else if" again.
Thanks for the idea though.
"Frank Le" <NOSPAM...@programmer.net> wrote in message
news:#7n7Ctui$GA....@cppssbbsa02.microsoft.com...
> Why not simply use if-else?
>
> Frank
>
In your particular situation, it appears that the possible values of your
CStringInputMonth are all uppercase and are all 3 letters long. Then you can
do this:
//I use lowercase, but it's irrespective.
CString AllMonths(".jan.feb.mar.apr.may"); ///all months; each
one starts at an index divisable by 4.
int iMonth = AllMonths.Find("."+stMonth); ///position of the
month in the AllMonths string.
iMonth = iMonth / 4+1; ///obtain the
actual month index.
That's it. Or just
int iMonth = AllMonths.Find("."+stMonth")/4 + 1;
and you'll only have 2 lines of code, instead of 3 * 12. You can remove the
"." from AllMonths and the call to Find(), and then replace the 4 with a 3.
AllMonths becomes "janfebmaraprmay...". The dot would be useful if "anf" was
a month of it's own, for e.g.
The defines:
// An attempt to duplicate the switch/case statements for use with non-integral
times.
#define STR_SWITCH(x) {CString switchval(x); if (false) {;} // Semicolon is
necessary
#define CASE(y) else if (switchval == y) {;
#define DEFAULT else if (true) {;
#define BREAK }
#define END_SWITCH }
An example:
STR_SWITCH(sName)
CASE(_T("John"))
// code
BREAK
CASE(_T("Bob"))
// code
BREAK
CASE(_T("George"))
// code
BREAK
CASE(_T("Fred"))
// code
BREAK
CASE(_T("Harry"))
// code
BREAK
DEFAULT
return -1;
BREAK
END_SWITCH;
John