Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CString in switch??

388 views
Skip to first unread message

David J. Zook

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
I want to use a CString variable in a switch statement, with simples strings
as case labels.

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 Le

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
Why not simply use if-else?

Frank

"David J. Zook" <dave...@my-deja.com> wrote in message
news:OVl0MZui$GA....@cppssbbsa02.microsoft.com...

John Smith

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
It's my understanding that only enumarated types (int, enum) can be used in
switch statement.

David J. Zook

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
I ended using if ... then ... else ... if ..., which is not too bad for
month names, but is terrible for strings that are app-specific. Maintenance
programmers are required to redevelop the logic from what is very nearly
spaghetti code.

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
>

Oleg Musteata

unread,
Mar 10, 2000, 3:00:00 AM3/10/00
to
You can only switch by simple, "ordered" types, such as int or char. And
that's with any c++ compiler. You can switch on a string in JavaScript1.3,
for e.g.

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.

John Fisher

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
David,
In the past, I've used the following to make things more readable than the "else
if" structure string comparisons require.

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

0 new messages