Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-2008, 03:28 PM
sat_andi@yahoo.co.uk
 
Posts: n/a
default option at the beginning of switch

How is this program supposed to behave? Is this even valid?

The default option is placed at the front of the switch statement,
modifying the original value:

#include <iostream>
using namespace std;

enum ENumber
{
eNum1 = 0,
eNum2,
eNum3
};

void TestSwitch(ENumber eNum)
{
switch(eNum)
{
default:
eNum = static_cast<ENumber>(eNum3 - eNum);
case eNum1:
cout << eNum << " mapped to: " << eNum1 << endl;
break;
case eNum2:
cout << eNum << " mapped to: " << eNum2 << endl;
break;
case eNum3:
cout << eNum << " mapped to: " << eNum3 << endl;
break;
}
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-20-2008, 09:58 PM
Bo Persson
 
Posts: n/a
Re: default option at the beginning of switch

sat_andi@yahoo.co.uk wrote:
> How is this program supposed to behave? Is this even valid?


I don't know how it is supposed to behave, but it is syntactically
correct. The order of the labels are not important.

> The default option is placed at the front of the switch statement,
> modifying the original value:


It can modify the value if it wants to, as the switch (case selection)
has already executed at that point. It will definitely not re-select a
case. However, as there is no break for the default case, it will fall
through to case eNum1. Is that really intentional??

>
> #include <iostream>
> using namespace std;
>
> enum ENumber
> {
> eNum1 = 0,
> eNum2,
> eNum3
> };
>
> void TestSwitch(ENumber eNum)
> {
> switch(eNum)
> {
> default:
> eNum = static_cast<ENumber>(eNum3 - eNum);
> case eNum1:
> cout << eNum << " mapped to: " << eNum1 << endl;
> break;
> case eNum2:
> cout << eNum << " mapped to: " << eNum2 << endl;
> break;
> case eNum3:
> cout << eNum << " mapped to: " << eNum3 << endl;
> break;
> }
> }



Bo Persson



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-21-2008, 02:40 AM
Hakusa@gmail.com
 
Posts: n/a
Re: default option at the beginning of switch

On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote:
> How is this program supposed to behave? Is this even valid?


Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is
not one of the answers, you fix it, but go to the first case listed.
The GCC assembly code shows that a switch statement is implemented as
a bunch of if and goto statements. The code has already passed the
gotos by the time they ever get to default and will not go back to
them.

Here's my suggestion, if you REALLY do want what you think you do.

void TestSwitch(ENumber eNum)
{
switch( eNum )
{
case valid_ans: break;
default: TestSwitch( /* make eNum valid * / );
}
}

But, then again, my suggestion could cause an infinite loop if your
solution for making eNum valid fails. Might want to make a testing
variable that auto-returns if there were too many iterations.

>void TestSwitch(ENumber eNum)
>{
> switch(eNum)
> {
> default:
> eNum = static_cast<ENumber>(eNum3 - eNum);

....
> }
>}


The big logical flaw here is what makes eNum invalid. If it's less
than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE
wrong! If it's greater than 2, then 2 - x will be negative! No matter
what, this won't make eNum valid; rethink it.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-22-2008, 01:58 PM
Nick Hounsome
 
Posts: n/a
Re: default option at the beginning of switch

On 21 Aug, 02:40, "Hak...@gmail.com" <Hak...@gmail.com> wrote:
> On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote:
>
> > How is this program supposed to behave? Is this even valid?

>
> Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is
> not one of the answers, you fix it, but go to the first case listed.
> The GCC assembly code shows that a switch statement is implemented as
> a bunch of if and goto statements. The code has already passed the
> gotos by the time they ever get to default and will not go back to
> them.
>
> Here's my suggestion, if you REALLY do want what you think you do.
>
> void TestSwitch(ENumber eNum)
> {
> switch( eNum )
> {
> case valid_ans: break;
> default: TestSwitch( /* make eNum valid * / );
> }
>
> }
>
> But, then again, my suggestion could cause an infinite loop if your
> solution for making eNum valid fails. Might want to make a testing
> variable that auto-returns if there were too many iterations.
>
>
>
> >void TestSwitch(ENumber eNum)
> >{
> > switch(eNum)
> > {
> > default:
> > eNum = static_cast<ENumber>(eNum3 - eNum);

> ...
> > }
> >}

>
> The big logical flaw here is what makes eNum invalid. If it's less
> than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE
> wrong! If it's greater than 2, then 2 - x will be negative! No matter
> what, this won't make eNum valid; rethink it.


{ Edits: quoted clc++m banner removed. Since the banner is automatically
appended to every article there is no need to quote it. -mod }

My guess is that it is a poor attempt to convert negative values into
Enums greater than the max one (Enum3).
As stated above, this fails if eNum > eNum3 initially but even if that
isn't a problem it would be more clearly written as
eNum = static_cast<ENumber>( eNum < 0 ? (eNum3-eNum) : eNum ); // or
expand to 'if' if you prefer


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT. The time now is 04:24 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Cheap Digital Cameras | Credit Counseling | Psychic Readings | Buy Anything On eBay | Credit Cards



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114