473,767 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch-case problem

I encountered a strange problem about switch-case statement.
switch(cmd)
{
case 1: statements; break;
case 2: statements; break;
... ....
case 11: S1;
S2;
S3;
statements;
break;
... ...
case xx
}

When the cmd is above 11, the first two statements are skipped. Take case 11
as example
the first executing statement is S3, not S1.
What the problem is?
Nov 14 '05 #1
10 3719
"Chih-Hsu Yen" <zs****@ms14.hi net.net> wrote:
I encountered a strange problem about switch-case statement.
switch(cmd)
{
case 1: statements; break;
case 2: statements; break;
... ....
case 11: S1;
S2;
S3;
statements;
break;
... ...
case xx
}

When the cmd is above 11, the first two statements are skipped. Take case 11
as example
the first executing statement is S3, not S1.
What the problem is?


I've no idea, and it would be very hard to tell without seeing your
actual code. For example, what _are_ S1, S2, and S3? How is cmd defined?
What is the case just before case 11? Show your real code, and we may be
able to help.

Richard
Nov 14 '05 #2
Most cases have analogous format as case 10, so I only pasted case 10.
It seems that the first two statements are skipped, no matter what they are,
while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in case 10,
but I can not figure out how could this happen.

-------------
scanf("%d",&cmd );

switch(cmd){
case 1: similar as case 10

case 2: similar as case 10

case 3: similar as case 10

case 10: /*insert inbound SPD*/
spd_udp.opcode = SPD_IN_INSERT;
printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
scanf("%x %x",&addr1,&add r2);
printf("please select an action : \n");
printf("1: bypass \n");
printf("2: IPsec \n");
printf("3: disard \n");
scanf("%d",&sel );
if(sel == 1)
{
entry_spd.ctrl. action_index = 0;
entry_spd.ctrl. next_index = 0;
}
else if(sel == 2)
{
entry_spd.ctrl. action_index = 64;
entry_spd.ctrl. next_index = 0;
}
else if(sel == 3)
{
entry_spd.ctrl. action_index = 128;
entry_spd.ctrl. next_index = 0;
}
else
{
printf("error selection!! \n");
break;
}
spd_udp.oper.op erand_addr1 = addr1;
spd_udp.oper.op erand_addr2 = addr2;
sendstring[0] = spd_udp.opcode;
sendstring[1] = spd_udp.oper.op erand_addr1;
sendstring[2] = spd_udp.oper.op erand_addr2;
sendstring[3] = entry_spd.ctrl. action_index;
sendstring[4] = entry_spd.ctrl. next_index;
sendstring[5] = 0;
sendstring[6] = entry_spd.proto col;
printf("please input sip value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
entry_spd.sip = word_build(b[0],b[1],b[2],b[3]);
for (i=7; i<11; i++)
{
sendstring[i] = b[i-7];
}
printf("please input sip Mask value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
entry_spd.sipMa sk = word_build(b[0],b[1],b[2],b[3]);
for (i=11; i<15; i++)
{
sendstring[i] = b[i-11];
}
printf("please input dip value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
entry_spd.dip = word_build(b[0],b[1],b[2],b[3]);
for (i=15; i<19; i++)
{
sendstring[i] = b[i-16];
}
printf("please input dip Mask value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
entry_spd.dipMa sk = word_build(b[0],b[1],b[2],b[3]);
for (i=19; i<23; i++)
{
sendstring[i] = b[i-19];
}
printf("please input spMaxMin value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);;
entry_spd.spMax Min = word_build(b[0],b[1],b[2],b[3]);
for (i=23; i<27; i++)
{
sendstring[i] = b[i-23];
}
printf("please input dpMaxMin value (XX XX XX XX, XX=00-FF): ");
scanf("%x %x %x %x", &b[0], &b[1], &b[2], &b[3]);
entry_spd.dpMax Min = word_build(b[0],b[1],b[2],b[3]);
for (i=27; i<31; i++)
{
sendstring[i] = b[i-27];
}
insert_isp(spd_ udp.oper,entry_ spd);
sendsocket(send string,SPD_INTP AC_LEN);
c=getchar();
break;
}
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> ¼¶¼g©ó¶l¥ó·s»D: 42************* **@news.xs4all. nl...
"Chih-Hsu Yen" <zs****@ms14.hi net.net> wrote:
I encountered a strange problem about switch-case statement.
switch(cmd)
{
case 1: statements; break;
case 2: statements; break;
... ....
case 11: S1;
S2;
S3;
statements;
break;
... ...
case xx
}

When the cmd is above 11, the first two statements are skipped. Take case
11
as example
the first executing statement is S3, not S1.
What the problem is?


I've no idea, and it would be very hard to tell without seeing your
actual code. For example, what _are_ S1, S2, and S3? How is cmd defined?
What is the case just before case 11? Show your real code, and we may be
able to help.

Richard

Nov 14 '05 #3

Chih-Hsu Yen wrote:
Most cases have analogous format as case 10, so I only pasted case 10. It seems that the first two statements are skipped, no matter what they are, while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in case 10, but I can not figure out how could this happen.


Could you by any means step through your code and see what is going on?
<snip code>

--
Imanpreet Singh Arora

Nov 14 '05 #4
It's really strange. I marked all statements as comments and left "break"
statement only in case 10.
However, the first two statements of case 11 are still skipped.
But, if I remove all statements and left "break" statement in case 10.
The execution of case 11 is correct.
Do there have restrictions on the number of lines in switch-case statement,
no matter what they are statements or comments?

"Minti" <im*******@gmai l.com>
???????:11***** *************** *@z14g2000cwz.g ooglegroups.com ...

Chih-Hsu Yen wrote:
Most cases have analogous format as case 10, so I only pasted case

10.
It seems that the first two statements are skipped, no matter what

they are,
while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in

case 10,
but I can not figure out how could this happen.


Could you by any means step through your code and see what is going on?
<snip code>

--
Imanpreet Singh Arora

Nov 14 '05 #5
*** top-posting corrected ***
Chih-Hsu Yen wrote:
"Minti" <im*******@gmai l.com>
Chih-Hsu Yen wrote:

Most cases have analogous format as case 10, so I only pasted
case 10. It seems that the first two statements are skipped, no
matter what they are, while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes
in case 10, but I can not figure out how could this happen.


Could you by any means step through your code and see what is
going on?


It's really strange. I marked all statements as comments and left
"break" statement only in case 10. However, the first two
statements of case 11 are still skipped. But, if I remove all
statements and left "break" statement in case 10. The execution
of case 11 is correct. Do there have restrictions on the number
of lines in switch-case statement, no matter what they are
statements or comments?


You almost certainly have undefined behaviour somewhere, which
might even be writing into a jump table. Simplify your code so
that errors stand out. That usually means splitting off
subroutines to do jobs.

Please do not top-post. Your answer belongs after, or intermixed
with, the material to which you reply, with non-germane portions
snipped out. Top-posting is generally considered rude, boorish and
inconsiderate.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #6
"Chih-Hsu Yen" <zs****@ms14.hi net.net> writes:
Most cases have analogous format as case 10, so I only pasted case 10.
It seems that the first two statements are skipped, no matter what they are,
while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in case 10,
but I can not figure out how could this happen.

-------------
scanf("%d",&cmd );
now a "\n" is still in the input stream...
switch(cmd){
case 1: similar as case 10

case 2: similar as case 10

case 3: similar as case 10

case 10: /*insert inbound SPD*/
spd_udp.opcode = SPD_IN_INSERT;
printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
scanf("%x %x",&addr1,&add r2);


this scanf is "skipped" due to the "\n" in the input stream...

.... an so on

Björn
--
Bjoern Pedersen

Nov 14 '05 #7

Bjoern Pedersen wrote:
"Chih-Hsu Yen" <zs****@ms14.hi net.net> writes:
Most cases have analogous format as case 10, so I only pasted case 10. It seems that the first two statements are skipped, no matter what they are, while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in case 10, but I can not figure out how could this happen.

-------------
scanf("%d",&cmd );


now a "\n" is still in the input stream...
switch(cmd){
case 1: similar as case 10

case 2: similar as case 10

case 3: similar as case 10

case 10: /*insert inbound SPD*/
spd_udp.opcode = SPD_IN_INSERT;
printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
scanf("%x %x",&addr1,&add r2);


this scanf is "skipped" due to the "\n" in the input stream...


Nopes, the scanf here won't be skipped. It would be skipped only if we
were taking a character which it isn't taking.

It seems to be different problem altogether. I believe that the OP is,
just doing something else that makes him believe that the the
statements are skipped.

--
Imanpreet Singh Arora

Nov 14 '05 #8
Chih-Hsu Yen wrote:
Most cases have analogous format as case 10, so I only pasted case 10.
It seems that the first two statements are skipped, no matter what they are,
while the case number is above 10.
Therefore, I thought that the problem may be caused by the codes in case 10,
but I can not figure out how could this happen.
[...] switch(cmd){
case 1: similar as case 10

case 2: similar as case 10

case 3: similar as case 10

case 10: /*insert inbound SPD*/
spd_udp.opcode = SPD_IN_INSERT; [...] sendsocket(send string,SPD_INTP AC_LEN);
c=getchar();
break;
}


Did you really close your switch after case 10, or can we assume that
case 11 starts after the break and before the brace?
Nov 14 '05 #9

In article <m3************ @resi2.office.f rm2>, Bjoern Pedersen <Bj************ *@frm2.tum.de> writes:
"Chih-Hsu Yen" <zs****@ms14.hi net.net> writes:
case 10: /*insert inbound SPD*/
spd_udp.opcode = SPD_IN_INSERT;
printf("please input the addr1 & addr2 (XX XX, XX=00-3F): ");
scanf("%x %x",&addr1,&add r2);


this scanf is "skipped" due to the "\n" in the input stream...


And the output from the printf will likely not have appeared yet,
because stdout is probably in line-buffering mode, no newline has
been written, and stdout has not been flushed.

We might have more confidence in claims about statements being
"skipped" if they were written to have visible effects, and if you
checked whether they failed. (scanf returns a value for a reason.)

C seems to attract the sort of programmer who throws code at a
problem and hopes it will all work correctly. This approach does not
always produce ideal results.

--
Michael Wojcik mi************@ microfocus.com

The antics which have been drawn together in this book are huddled here
for mutual protection like sheep. If they had half a wit apiece each
would bound off in many directions, to unsimplify the target. -- Walt Kelly
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2620
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
2
6247
by: jr | last post by:
I have a niggle with the Switch function I have a querey which has a column with 3 digit values of which there are about 20 which are unique. These are meaningless to the user and so using the switch function I assign a string to each value. theus alias
13
6215
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the microcontroller should display in the LCD the value of the voltage applied to the input of the ADC. The above procedure should only execute once the user has entered "1234" using a keypad that is attached to the 8051 microprocessor.
12
3233
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
65
6693
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? In terms of generated machine code, how does hundreds of cases in a switch differ from hundreds of if-elses? Do compilers or processors do any optimization on such structured code? Do I need to worry about the performance, usually?
13
7468
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
2
4690
by: 7777777.chen | last post by:
Is it true that VC++ doesn't support switch on string data type? Did anyone know how to handle the following situation? (1) System::String* s_TC = treeview->Nodes->Item->Nodes->Item->Text; or (2) std::string s_TC = string_to_charP(treeview->Nodes->Item->Nodes->Item->Text); Switch (s_TC) {
13
7555
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to else-if. I know to put the if statement that is most likely to be true at the top of the else-if chain -so as to minimize checks. I've searched around online and mainly found answers to this question for Java programmers. Thanks in advance,...
11
10863
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is a better way of doing this? if ("control" == commandString) { } else if ("execute" == commandString)
11
4966
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the statements in switch #2 enter a 3rd switch. I don't receive any compile errors except whenever I attempt to add default switches to switches 2 or 3.
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.