Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 'if' statement to control switch case

Reply
Thread Tools

'if' statement to control switch case

 
 
John
Guest
Posts: n/a
 
      04-11-2007
Hi Folks,

I'm experimenting a little with creating a custom CEdit control so that
I can decide on what the user is allowed to type into the control. I
started off only allowing floating point numbers then added support for
putting in lat/lon coordinates.

I tried this little piece of code inside the OnChar function but
compiler complained about missing ';' after "case _T('W'):"

switch(nChar)
{
if(m_bLATLON) // if latlon then no negs and 4 extra chars.
{
case _T('N'):
case _T('E'):
case _T('S'):
case _T('W'):
}
else
case _T('-'); // if not latlon we are allowed negs.

case _T('.'): // allowed characters on both cases.
case _T('0'):
case _T('1'):
case _T('2'):
case _T('3'):
case _T('4'):
case _T('5'):
case _T('6'):
case _T('7'):
case _T('8'):
case _T('9'):
case _T('\b'):
break;

default:
return;
}

now I could just have the 'if' statement first and then have two
seperate switch statements but i would rather do the method above as it
'looks' more elegante )
Is there some magic way to do what I am wanting or does it just break
the rules of c++ ?

Cheers,

John.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-11-2007
John wrote:
> Hi Folks,
>
> I'm experimenting a little with creating a custom CEdit control so
> that I can decide on what the user is allowed to type into the
> control. I started off only allowing floating point numbers then
> added support for putting in lat/lon coordinates.
>
> I tried this little piece of code inside the OnChar function but
> compiler complained about missing ';' after "case _T('W'):"
>
> switch(nChar)
> {
> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
> {
> case _T('N'):
> case _T('E'):
> case _T('S'):
> case _T('W'):


Have you tried inserting the semicolon here?

> }
> else
> case _T('-'); // if not latlon we are allowed negs.


What's the semicolon doing here?

>
> case _T('.'): // allowed characters on both cases.
> case _T('0'):
> case _T('1'):
> case _T('2'):
> case _T('3'):
> case _T('4'):
> case _T('5'):
> case _T('6'):
> case _T('7'):
> case _T('8'):
> case _T('9'):
> case _T('\b'):
> break;
>
> default:
> return;
> }


Next time, do please post the real code that gives you trouble.

>
> now I could just have the 'if' statement first and then have two
> seperate switch statements but i would rather do the method above as
> it 'looks' more elegante )
> Is there some magic way to do what I am wanting or does it just break
> the rules of c++ ?


I think it does break the rules, but you haven't taken it to the full
extreme yet.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      04-11-2007
Victor Bazarov wrote:
> John wrote:
>
>>Hi Folks,
>>
>>I'm experimenting a little with creating a custom CEdit control so
>>that I can decide on what the user is allowed to type into the
>>control. I started off only allowing floating point numbers then
>>added support for putting in lat/lon coordinates.
>>
>>I tried this little piece of code inside the OnChar function but
>>compiler complained about missing ';' after "case _T('W'):"
>>
>>switch(nChar)
>>{
>> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
>> {
>> case _T('N'):
>> case _T('E'):
>> case _T('S'):
>> case _T('W'):

>
>
> Have you tried inserting the semicolon here?


Yes, and the error goes away but the functionality of the code doesn't
work i.e. regardless of whether m_bLATLON is true/false it never
acknowledges any of the cases.

>
>
>> }
>> else
>> case _T('-'); // if not latlon we are allowed negs.

>
>
> What's the semicolon doing here?


mmmhhh not sure - its not in my code so must just be a typo.

>
>
>> case _T('.'): // allowed characters on both cases.
>> case _T('0'):
>> case _T('1'):
>> case _T('2'):
>> case _T('3'):
>> case _T('4'):
>> case _T('5'):
>> case _T('6'):
>> case _T('7'):
>> case _T('8'):
>> case _T('9'):
>> case _T('\b'):
>> break;
>>
>> default:
>> return;
>>}

>
>
> Next time, do please post the real code that gives you trouble.


This is the real code - it just had that one typo. I didn't want to post
the entire OnChar function that I overode as I thought someone would
see that it was a CEdit function and flame me for posting in the wrong
group!


>
>
>>now I could just have the 'if' statement first and then have two
>>seperate switch statements but i would rather do the method above as
>>it 'looks' more elegante )
>>Is there some magic way to do what I am wanting or does it just break
>>the rules of c++ ?

>
>
> I think it does break the rules, but you haven't taken it to the full
> extreme yet.
>
> V

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-11-2007
John wrote:
> Victor Bazarov wrote:
>> John wrote:
>>
>>> Hi Folks,
>>>
>>> I'm experimenting a little with creating a custom CEdit control so
>>> that I can decide on what the user is allowed to type into the
>>> control. I started off only allowing floating point numbers then
>>> added support for putting in lat/lon coordinates.
>>>
>>> I tried this little piece of code inside the OnChar function but
>>> compiler complained about missing ';' after "case _T('W'):"
>>>
>>> switch(nChar)
>>> {
>>> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
>>> {
>>> case _T('N'):
>>> case _T('E'):

[..]

OK, this is what I tried. It compiles (on both VC++ and Comeau),
and the effect (on VC++) is that the 'if' is essentially ignored:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
switch (argc)
{
if (argv[0][0] == 'C') {
case 0:
case 1:
cout << "0 or 1\n";
break;
}
else {
case 2:
cout << "2\n";
break;
default:
cout << "3 or more\n";
}
}
}

Try it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      04-11-2007
Victor Bazarov wrote:
> John wrote:
>> Victor Bazarov wrote:
>>> John wrote:
>>>
>>>> Hi Folks,
>>>>
>>>> I'm experimenting a little with creating a custom CEdit control so
>>>> that I can decide on what the user is allowed to type into the
>>>> control. I started off only allowing floating point numbers then
>>>> added support for putting in lat/lon coordinates.
>>>>
>>>> I tried this little piece of code inside the OnChar function but
>>>> compiler complained about missing ';' after "case _T('W'):"
>>>>
>>>> switch(nChar)
>>>> {
>>>> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
>>>> {
>>>> case _T('N'):
>>>> case _T('E'):

> [..]
>
> OK, this is what I tried. It compiles (on both VC++ and Comeau),
> and the effect (on VC++) is that the 'if' is essentially ignored:
>


Indeed. There's no way that the control flow can reach it, since the
switch statement jumps to a case label, and there's no label before the if.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 7:18 am, John <(E-Mail Removed)> wrote:
> Hi Folks,
>
> I'm experimenting a little with creating a custom CEdit control so that
> I can decide on what the user is allowed to type into the control. I
> started off only allowing floating point numbers then added support for
> putting in lat/lon coordinates.
>
> I tried this little piece of code inside the OnChar function but
> compiler complained about missing ';' after "case _T('W'):"
>
> switch(nChar)
> {
> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
> {
> case _T('N'):
> case _T('E'):
> case _T('S'):
> case _T('W'):
> }
> else
> case _T('-'); // if not latlon we are allowed negs.
>
> case _T('.'): // allowed characters on both cases.
> case _T('0'):
> case _T('1'):
> case _T('2'):
> case _T('3'):
> case _T('4'):
> case _T('5'):
> case _T('6'):
> case _T('7'):
> case _T('8'):
> case _T('9'):
> case _T('\b'):
> break;
>
> default:
> return;
>
> }
>
> now I could just have the 'if' statement first and then have two
> seperate switch statements but i would rather do the method above as it
> 'looks' more elegante )


....and bloody confusing. Duff's Device did a similar trick for the
purposes of optimization, but I don't think optimization is your major
concern here (or at least it shouldn't be at this stage in your
development). Prefer to go with a straightforward, readable approach
even if it's less "cool". As Kernighan, co-creator of C, said,
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." That seems to apply
directly here.

Cheers! --M

 
Reply With Quote
 
Jonas
Guest
Posts: n/a
 
      04-11-2007

"John" <(E-Mail Removed)> wrote in message
newsu3Th.247$(E-Mail Removed)...
> Hi Folks,
>
> I'm experimenting a little with creating a custom CEdit control so that I
> can decide on what the user is allowed to type into the control. I started
> off only allowing floating point numbers then added support for putting in
> lat/lon coordinates.
>
> I tried this little piece of code inside the OnChar function but compiler
> complained about missing ';' after "case _T('W'):"
>
> switch(nChar)
> {
> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
> {
> case _T('N'):
> case _T('E'):
> case _T('S'):
> case _T('W'):
> }
> else
> case _T('-'); // if not latlon we are allowed negs.
>
> case _T('.'): // allowed characters on both cases.
> case _T('0'):
> case _T('1'):
> case _T('2'):
> case _T('3'):
> case _T('4'):
> case _T('5'):
> case _T('6'):
> case _T('7'):
> case _T('8'):
> case _T('9'):
> case _T('\b'):
> break;
>
> default:
> return;
> }


Wrap an if around your cases like that will not produce the desired effect.
Try conditionally returning or breaking depending on the value of m_bLATLON
instead:

switch(nChar)
{
case _T('N'):
case _T('E'):
case _T('S'):
case _T('W'):
if(!m_bLATLON)
return;
break;

case _T('-'):
if(m_bLATLON)
return;
break;

case _T('.'):
case _T('0'):
case _T('1'):
case _T('2'):
case _T('3'):
case _T('4'):
case _T('5'):
case _T('6'):
case _T('7'):
case _T('8'):
case _T('9'):
case _T('\b'):
break;

default:
return;
}

--
Jonas


 
Reply With Quote
 
John
Guest
Posts: n/a
 
      04-11-2007
Jonas wrote:
> "John" <(E-Mail Removed)> wrote in message
> newsu3Th.247$(E-Mail Removed)...
>
>>Hi Folks,
>>
>>I'm experimenting a little with creating a custom CEdit control so that I
>>can decide on what the user is allowed to type into the control. I started
>>off only allowing floating point numbers then added support for putting in
>>lat/lon coordinates.
>>
>>I tried this little piece of code inside the OnChar function but compiler
>>complained about missing ';' after "case _T('W'):"
>>
>>switch(nChar)
>>{
>> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
>> {
>> case _T('N'):
>> case _T('E'):
>> case _T('S'):
>> case _T('W'):
>> }
>> else
>> case _T('-'); // if not latlon we are allowed negs.
>>
>> case _T('.'): // allowed characters on both cases.
>> case _T('0'):
>> case _T('1'):
>> case _T('2'):
>> case _T('3'):
>> case _T('4'):
>> case _T('5'):
>> case _T('6'):
>> case _T('7'):
>> case _T('8'):
>> case _T('9'):
>> case _T('\b'):
>> break;
>>
>> default:
>> return;
>>}

>
>
> Wrap an if around your cases like that will not produce the desired effect.
> Try conditionally returning or breaking depending on the value of m_bLATLON
> instead:
>
> switch(nChar)
> {
> case _T('N'):
> case _T('E'):
> case _T('S'):
> case _T('W'):
> if(!m_bLATLON)
> return;
> break;
>
> case _T('-'):
> if(m_bLATLON)
> return;
> break;
>
> case _T('.'):
> case _T('0'):
> case _T('1'):
> case _T('2'):
> case _T('3'):
> case _T('4'):
> case _T('5'):
> case _T('6'):
> case _T('7'):
> case _T('8'):
> case _T('9'):
> case _T('\b'):
> break;
>
> default:
> return;
> }
>
> --
> Jonas
>
>


Hey Jonas,

thanks for the reply - I can't do it the way you wrote though.

I need all of the default cases too (. 0 1 2 3 ....etc) depending on the
m_bLATLON variable i wanted to extend them, not blank them out totally.


To the rest of the guys that replied - cheers for the help. I've just
decided to do it the long way and duplicate/customize the switch based
on an 'if-else' test on the m_bLATLON var.

Thanks everybody,

John.
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      04-11-2007
In article <Du3Th.247$(E-Mail Removed)>,
John <(E-Mail Removed)> wrote:
>I'm experimenting a little with creating a custom CEdit control so that
>I can decide on what the user is allowed to type into the control. I
>started off only allowing floating point numbers then added support for
>putting in lat/lon coordinates.
>
>I tried this little piece of code inside the OnChar function but
>compiler complained about missing ';' after "case _T('W'):"
>
>switch(nChar)
>{
> if(m_bLATLON) // if latlon then no negs and 4 extra chars.
> {
> case _T('N'):
> case _T('E'):
> case _T('S'):
> case _T('W'):


Labels in C++ and C need to target statements. A case establishes
a label. So you need at least a semicolon here, since that brings
forth a null statement.

> }
> else
> case _T('-'); // if not latlon we are allowed negs.


Same here. You need at least a : and ;

> case _T('.'): // allowed characters on both cases.
> case _T('0'):
> case _T('1'):
> case _T('2'):
> case _T('3'):
> case _T('4'):
> case _T('5'):
> case _T('6'):
> case _T('7'):
> case _T('8'):
> case _T('9'):
> case _T('\b'):
> break;
>
> default:
> return;
>}
>
>now I could just have the 'if' statement first and then have two
>seperate switch statements but i would rather do the method above as it
>'looks' more elegante )
>Is there some magic way to do what I am wanting or does it just break
>the rules of c++ ?


The fixed above should allow it to compile.

However, then you run into other problems, to wit,
that the if statement never executes as an if statement
(because it starts somewhere it will never get executed as such).
Your elegance (which is in the eye of the beholder) is too clever
for your own good.

That said, hoisting the if into at least 2 of the groups seems to fit,
but it's unclear what you are really trying to do, so that may be
misguided until you tell us.
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      04-11-2007
In article <sn7Th.463$(E-Mail Removed)>,
John <(E-Mail Removed)> wrote:
>Jonas wrote:
>> "John" <(E-Mail Removed)> wrote in message
>> newsu3Th.247$(E-Mail Removed)...
>>>I'm experimenting a little with creating a custom CEdit control so that I
>>>can decide on what the user is allowed to type into the control. I started
>>>off only allowing floating point numbers then added support for putting in
>>>lat/lon coordinates.
>>>
>>>I tried this little piece of code inside the OnChar function but compiler
>>>complained about missing ';' after "case _T('W'):"
>>>...

>> Wrap an if around your cases like that will not produce the desired effect.
>> Try conditionally returning or breaking depending on the value of m_bLATLON
>> instead:
>>
>> switch(nChar)
>> {
>> case _T('N'):
>> case _T('E'):
>> case _T('S'):
>> case _T('W'):
>> if(!m_bLATLON)
>> return;
>> break;
>>
>> case _T('-'):
>> if(m_bLATLON)
>> return;
>> break;
>>
>> case _T('.'):
>> case _T('0'):
>> case _T('1'):
>> case _T('2'):
>> case _T('3'):
>> case _T('4'):
>> case _T('5'):
>> case _T('6'):
>> case _T('7'):
>> case _T('8'):
>> case _T('9'):
>> case _T('\b'):
>> break;
>>
>> default:
>> return;
>> }
>>

>thanks for the reply - I can't do it the way you wrote though.
>
>I need all of the default cases too (. 0 1 2 3 ....etc) depending on the
>m_bLATLON variable i wanted to extend them, not blank them out totally.


Probably you can use it if you never the test Jonas shows and
emit errors instead of the return, but it's still unclear what
you're doing. As....

>To the rest of the guys that replied - cheers for the help. I've just
>decided to do it the long way and duplicate/customize the switch based
>on an 'if-else' test on the m_bLATLON var.


.... yes, actually it looks like you combined at least two distinct
things into one, at least as you've presented it. I mean,
can one really enter N55.W-?
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
Odd issue with Switch-Case statement in Code behind page. Guy Noir ASP .Net 2 11-02-2006 07:06 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
case/switch statement? Joe Stevenson Python 28 06-23-2005 12:42 PM
switch case statement vic Java 9 03-04-2004 04:59 PM



Advertisments